Program: 168920
Project: 168920
Buggy source:
protected StringBuilder applyCtes(StringBuilder sqlSb,Query baseQuery,boolean isSubquery,List<Query> participatingQueries){
  if (!isMainQuery || isSubquery || !cteManager.hasCtes() && statementType != DbmsStatementType.DELETE) {
    return null;
  }
  Map<String,String> tableNameRemapping=new LinkedHashMap<String,String>(0);
  StringBuilder sb=new StringBuilder(cteManager.getCtes().size() * 100);
  sb.append(dbmsDialect.getWithClause(cteManager.isRecursive()));
  sb.append("String_Node_Str");
  boolean firstCte=true;
  for (  CTEInfo cteInfo : 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);
      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 : 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);
  }
  return sb;
}

Fixed version (ground truth):
protected StringBuilder applyCtes(StringBuilder sqlSb,Query baseQuery,boolean isSubquery,List<Query> participatingQueries){
  if (!isMainQuery || isSubquery || !cteManager.hasCtes() && statementType != DbmsStatementType.DELETE) {
    return null;
  }
  Map<String,String> tableNameRemapping=new LinkedHashMap<String,String>(0);
  StringBuilder sb=new StringBuilder(cteManager.getCtes().size() * 100);
  sb.append(dbmsDialect.getWithClause(cteManager.isRecursive()));
  sb.append("String_Node_Str");
  boolean firstCte=true;
  for (  CTEInfo cteInfo : 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 : 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);
  }
  return sb;
}