Program: 168971
Project: 168971
Buggy source:
protected StringBuilder applyCtes(StringBuilder sqlSb,Query baseQuery,boolean isSubquery,List<Query> participatingQueries){
  if (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");
    }
    sb.append(cteInfo.name);
    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);
      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");
    final String subselect="String_Node_Str" + cteInfo.name + "String_Node_Str";
    int subselectIndex=0;
    while ((subselectIndex=sb.indexOf(subselect,subselectIndex)) > -1) {
      sb.replace(subselectIndex,subselectIndex + subselect.length(),cteInfo.name);
    }
    subselectIndex=0;
    while ((subselectIndex=sqlSb.indexOf(subselect,subselectIndex)) > -1) {
      sqlSb.replace(subselectIndex,subselectIndex + subselect.length(),cteInfo.name);
    }
  }
  firstCte=applyCascadingDelete(baseQuery,this,participatingQueries,sb,"String_Node_Str",firstCte);
  if (firstCte) {
    return null;
  }
  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 (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");
    }
    sb.append(cteInfo.name);
    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");
    final String subselect="String_Node_Str" + cteInfo.name + "String_Node_Str";
    int subselectIndex=0;
    while ((subselectIndex=sb.indexOf(subselect,subselectIndex)) > -1) {
      sb.replace(subselectIndex,subselectIndex + subselect.length(),cteInfo.name);
    }
    subselectIndex=0;
    while ((subselectIndex=sqlSb.indexOf(subselect,subselectIndex)) > -1) {
      sqlSb.replace(subselectIndex,subselectIndex + subselect.length(),cteInfo.name);
    }
  }
  firstCte=applyCascadingDelete(baseQuery,this,participatingQueries,sb,"String_Node_Str",firstCte);
  if (firstCte) {
    return null;
  }
  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;
}