Program: 168894
Project: 168894
Buggy source:
protected StringBuilder applyCtes(StringBuilder sqlSb,Query baseQuery,boolean isSubquery,List<Query> participatingQueries){
  if (!isMainQuery || isSubquery || !mainQuery.cteManager.hasCtes() && statementType != DbmsStatementType.DELETE || statementType != DbmsStatementType.SELECT && !mainQuery.dbmsDialect.supportsWithClauseInModificationQuery()) {
    return null;
  }
  Map<String,String> tableNameRemapping=new LinkedHashMap<String,String>(0);
  StringBuilder sb=new StringBuilder(mainQuery.cteManager.getCtes().size() * 100);
  sb.append(dbmsDialect.getWithClause(mainQuery.cteManager.isRecursive()));
  sb.append("String_Node_Str");
  boolean firstCte=true;
  for (  CTEInfo cteInfo : mainQuery.cteManager.getCtes()) {
    Map<DbmsModificationState,String> modificationStates=cteInfo.nonRecursiveCriteriaBuilder.getModificationStates(explicitVersionEntities);
    Query nonRecursiveQuery=cteInfo.nonRecursiveCriteriaBuilder.getQuery(modificationStates);
    participatingQueries.add(nonRecursiveQuery);
    Query recursiveQuery=null;
    if (cteInfo.recursive) {
      modificationStates=cteInfo.nonRecursiveCriteriaBuilder.getModificationStates(explicitVersionEntities);
      recursiveQuery=cteInfo.recursiveCriteriaBuilder.getQuery(modificationStates);
      if (!dbmsDialect.supportsJoinsInRecursiveCte() && cteInfo.recursiveCriteriaBuilder.joinManager.hasJoins()) {
        throw new IllegalStateException("String_Node_Str");
      }
      participatingQueries.add(recursiveQuery);
    }
    firstCte=applyCascadingDelete(nonRecursiveQuery,cteInfo.nonRecursiveCriteriaBuilder,participatingQueries,sb,cteInfo.name,firstCte);
    firstCte=applyAddedCtes(nonRecursiveQuery,cteInfo.nonRecursiveCriteriaBuilder,sb,tableNameRemapping,firstCte);
    firstCte=applyAddedCtes(recursiveQuery,cteInfo.recursiveCriteriaBuilder,sb,tableNameRemapping,firstCte);
    String cteNonRecursiveSqlQuery=getSql(nonRecursiveQuery);
    if (firstCte) {
      firstCte=false;
    }
 else {
      sb.append("String_Node_Str");
    }
    String cteName=cteInfo.cteType.getName();
    sb.append(cteName);
    sb.append('(');
    final List<String> attributes=cteInfo.attributes;
    boolean first=true;
    for (int i=0; i < attributes.size(); i++) {
      String[] columns=cbf.getExtendedQuerySupport().getColumnNames(em,cteInfo.cteType,attributes.get(i));
      for (      String column : columns) {
        if (first) {
          first=false;
        }
 else {
          sb.append("String_Node_Str");
        }
        sb.append(column);
      }
    }
    sb.append(')');
    sb.append("String_Node_Str");
    sb.append(cteNonRecursiveSqlQuery);
    if (cteInfo.recursive) {
      String cteRecursiveSqlQuery=getSql(recursiveQuery);
      if (cteInfo.unionAll) {
        sb.append("String_Node_Str");
      }
 else {
        sb.append("String_Node_Str");
      }
      sb.append(cteRecursiveSqlQuery);
    }
 else     if (!dbmsDialect.supportsNonRecursiveWithClause()) {
      sb.append("String_Node_Str");
      sb.append("String_Node_Str");
      sb.append("String_Node_Str");
      for (int i=1; i < attributes.size(); i++) {
        sb.append("String_Node_Str");
        sb.append("String_Node_Str");
      }
      sb.append("String_Node_Str");
    }
    sb.append("String_Node_Str");
  }
  firstCte=applyCascadingDelete(baseQuery,this,participatingQueries,sb,"String_Node_Str",firstCte);
  if (firstCte) {
    return null;
  }
  for (  CTEInfo cteInfo : mainQuery.cteManager.getCtes()) {
    String cteName=cteInfo.cteType.getName();
    final String subselect="String_Node_Str" + cteName + "String_Node_Str";
    int subselectIndex=0;
    while ((subselectIndex=sb.indexOf(subselect,subselectIndex)) > -1) {
      sb.replace(subselectIndex,subselectIndex + subselect.length(),cteName);
    }
    final String mainSubselect="String_Node_Str" + cteName + "String_Node_Str";
    subselectIndex=0;
    while ((subselectIndex=sqlSb.indexOf(mainSubselect,subselectIndex)) > -1) {
      sqlSb.replace(subselectIndex,subselectIndex + mainSubselect.length(),cteName);
    }
  }
  sb.append("String_Node_Str");
  for (  Map.Entry<String,String> tableNameRemappingEntry : tableNameRemapping.entrySet()) {
    String sqlAlias=cbf.getExtendedQuerySupport().getSqlAlias(em,baseQuery,tableNameRemappingEntry.getKey());
    String newCteName=tableNameRemappingEntry.getValue();
    applyTableNameRemapping(sqlSb,sqlAlias,newCteName,null);
  }
  return sb;
}

Fixed version (ground truth):
protected StringBuilder applyCtes(StringBuilder sqlSb,Query baseQuery,boolean isSubquery,List<Query> participatingQueries){
  if (!isMainQuery || isSubquery || !dbmsDialect.supportsWithClause()|| !mainQuery.cteManager.hasCtes() && statementType != DbmsStatementType.DELETE || statementType != DbmsStatementType.SELECT && !mainQuery.dbmsDialect.supportsWithClauseInModificationQuery()) {
    return null;
  }
  Map<String,String> tableNameRemapping=new LinkedHashMap<String,String>(0);
  StringBuilder sb=new StringBuilder(mainQuery.cteManager.getCtes().size() * 100);
  sb.append(dbmsDialect.getWithClause(mainQuery.cteManager.isRecursive()));
  sb.append("String_Node_Str");
  boolean firstCte=true;
  for (  CTEInfo cteInfo : mainQuery.cteManager.getCtes()) {
    Map<DbmsModificationState,String> modificationStates=cteInfo.nonRecursiveCriteriaBuilder.getModificationStates(explicitVersionEntities);
    Query nonRecursiveQuery=cteInfo.nonRecursiveCriteriaBuilder.getQuery(modificationStates);
    participatingQueries.add(nonRecursiveQuery);
    Query recursiveQuery=null;
    if (cteInfo.recursive) {
      modificationStates=cteInfo.nonRecursiveCriteriaBuilder.getModificationStates(explicitVersionEntities);
      recursiveQuery=cteInfo.recursiveCriteriaBuilder.getQuery(modificationStates);
      if (!dbmsDialect.supportsJoinsInRecursiveCte() && cteInfo.recursiveCriteriaBuilder.joinManager.hasJoins()) {
        throw new IllegalStateException("String_Node_Str");
      }
      participatingQueries.add(recursiveQuery);
    }
    firstCte=applyCascadingDelete(nonRecursiveQuery,cteInfo.nonRecursiveCriteriaBuilder,participatingQueries,sb,cteInfo.name,firstCte);
    firstCte=applyAddedCtes(nonRecursiveQuery,cteInfo.nonRecursiveCriteriaBuilder,sb,tableNameRemapping,firstCte);
    firstCte=applyAddedCtes(recursiveQuery,cteInfo.recursiveCriteriaBuilder,sb,tableNameRemapping,firstCte);
    String cteNonRecursiveSqlQuery=getSql(nonRecursiveQuery);
    if (firstCte) {
      firstCte=false;
    }
 else {
      sb.append("String_Node_Str");
    }
    String cteName=cteInfo.cteType.getName();
    sb.append(cteName);
    sb.append('(');
    final List<String> attributes=cteInfo.attributes;
    boolean first=true;
    for (int i=0; i < attributes.size(); i++) {
      String[] columns=cbf.getExtendedQuerySupport().getColumnNames(em,cteInfo.cteType,attributes.get(i));
      for (      String column : columns) {
        if (first) {
          first=false;
        }
 else {
          sb.append("String_Node_Str");
        }
        sb.append(column);
      }
    }
    sb.append(')');
    sb.append("String_Node_Str");
    sb.append(cteNonRecursiveSqlQuery);
    if (cteInfo.recursive) {
      String cteRecursiveSqlQuery=getSql(recursiveQuery);
      if (cteInfo.unionAll) {
        sb.append("String_Node_Str");
      }
 else {
        sb.append("String_Node_Str");
      }
      sb.append(cteRecursiveSqlQuery);
    }
 else     if (!dbmsDialect.supportsNonRecursiveWithClause()) {
      sb.append("String_Node_Str");
      sb.append("String_Node_Str");
      sb.append("String_Node_Str");
      for (int i=1; i < attributes.size(); i++) {
        sb.append("String_Node_Str");
        sb.append("String_Node_Str");
      }
      sb.append("String_Node_Str");
    }
    sb.append("String_Node_Str");
  }
  firstCte=applyCascadingDelete(baseQuery,this,participatingQueries,sb,"String_Node_Str",firstCte);
  if (firstCte) {
    return null;
  }
  for (  CTEInfo cteInfo : mainQuery.cteManager.getCtes()) {
    String cteName=cteInfo.cteType.getName();
    final String subselect="String_Node_Str" + cteName + "String_Node_Str";
    int subselectIndex=0;
    while ((subselectIndex=sb.indexOf(subselect,subselectIndex)) > -1) {
      sb.replace(subselectIndex,subselectIndex + subselect.length(),cteName);
    }
    final String mainSubselect="String_Node_Str" + cteName + "String_Node_Str";
    subselectIndex=0;
    while ((subselectIndex=sqlSb.indexOf(mainSubselect,subselectIndex)) > -1) {
      sqlSb.replace(subselectIndex,subselectIndex + mainSubselect.length(),cteName);
    }
  }
  sb.append("String_Node_Str");
  for (  Map.Entry<String,String> tableNameRemappingEntry : tableNameRemapping.entrySet()) {
    String sqlAlias=cbf.getExtendedQuerySupport().getSqlAlias(em,baseQuery,tableNameRemappingEntry.getKey());
    String newCteName=tableNameRemappingEntry.getValue();
    applyTableNameRemapping(sqlSb,sqlAlias,newCteName,null);
  }
  return sb;
}