Program: 140698
Project: 140698
Buggy source:
/** 
 * DOC scorreia Comment method "createSqlQuery".
 * @param dataFilterExpression
 * @param analysis
 * @param indicator
 * @throws ParseException
 * @throws AnalysisExecutionException
 */
private boolean createSqlQuery(String dataFilterAsString,Indicator indicator) throws ParseException, AnalysisExecutionException {
  ModelElement analyzedElement=indicator.getAnalyzedElement();
  if (analyzedElement == null) {
    return traceError("String_Node_Str" + indicator.getName());
  }
  TdColumn tdColumn=SwitchHelpers.COLUMN_SWITCH.doSwitch(indicator.getAnalyzedElement());
  if (tdColumn == null) {
    return traceError("String_Node_Str" + indicator.getName());
  }
  String colName=quote(tdColumn.getName());
  if (!belongToSameSchemata(tdColumn)) {
    StringBuffer buf=new StringBuffer();
    for (    orgomg.cwm.objectmodel.core.Package schema : schemata.values()) {
      buf.append(schema.getName() + "String_Node_Str");
    }
    log.error("String_Node_Str" + colName + "String_Node_Str"+ buf.toString().trim()+ "String_Node_Str");
    return false;
  }
  colName=castColumn(indicator,tdColumn,colName);
  String language=dbms().getDbmsName();
  Expression sqlGenericExpression=null;
  IndicatorDefinition indicatorDefinition=indicator.getIndicatorDefinition();
  if (indicatorDefinition == null) {
    return traceError("String_Node_Str" + indicator.getName());
  }
  sqlGenericExpression=getSqlExpression(indicatorDefinition,language);
  if (sqlGenericExpression == null) {
    log.warn("String_Node_Str" + language + "String_Node_Str"+ indicatorDefinition.getName());
    if (log.isInfoEnabled()) {
      log.info("String_Node_Str" + dbms().getDefaultLanguage());
    }
    sqlGenericExpression=getSqlExpression(indicatorDefinition,dbms().getDefaultLanguage());
  }
  if (sqlGenericExpression == null || sqlGenericExpression.getBody() == null) {
    return traceError("String_Node_Str" + (indicator.getName() != null ? indicator.getName() : indicator.eClass().getName()) + "String_Node_Str"+ ResourceHelper.getUUID(indicatorDefinition));
  }
  List<String> whereExpression=new ArrayList<String>();
  if (StringUtils.isNotBlank(dataFilterAsString)) {
    whereExpression.add(dataFilterAsString);
  }
  List<String> rangeStrings=null;
  DateGrain dateAggregationType=null;
  IndicatorParameters parameters=indicator.getParameters();
  if (parameters != null) {
    Domain bins=parameters.getBins();
    if (bins != null) {
      rangeStrings=getBinsAsGenericString(bins.getRanges());
    }
    DateParameters dateParameters=parameters.getDateParameters();
    if (dateParameters != null) {
      dateAggregationType=dateParameters.getDateAggregationType();
    }
    TextParameters textParameter=parameters.getTextParameter();
    if (textParameter != null) {
      if (textParameter.isIgnoreCase()) {
        colName=dbms().toUpperCase(colName);
      }
      if (!textParameter.isUseBlank()) {
        whereExpression.add(dbms().isNotBlank(colName));
      }
      if (textParameter.isUseNulls()) {
        colName=dbms().replaceNullsWithString(colName,"String_Node_Str");
      }
    }
  }
  String table=quote(ColumnHelper.getColumnSetFullName(tdColumn));
  String catalogName=getCatalogName(tdColumn);
  table=dbms().toQualifiedName(catalogName,null,table);
  String completedSqlString=null;
  if (indicator.eClass().equals(IndicatorsPackage.eINSTANCE.getMedianIndicator()) || indicator.eClass().equals(IndicatorsPackage.eINSTANCE.getLowerQuartileIndicator()) || indicator.eClass().equals(IndicatorsPackage.eINSTANCE.getUpperQuartileIndicator())) {
    completedSqlString=getCompletedStringForQuantiles(indicator,sqlGenericExpression,colName,table,whereExpression);
    completedSqlString=addWhereToSqlStringStatement(whereExpression,completedSqlString);
  }
 else   if (indicator.eClass().equals(IndicatorsPackage.eINSTANCE.getFrequencyIndicator()) || indicator.eClass().equals(IndicatorsPackage.eINSTANCE.getModeIndicator())) {
    int topN=indicator.eClass().equals(IndicatorsPackage.eINSTANCE.getModeIndicator()) ? 1 : getTopN(indicator);
    if (topN <= 0) {
      topN=TOP_N;
    }
    if (rangeStrings != null) {
      completedSqlString=getUnionCompletedString(indicator,sqlGenericExpression,colName,table,whereExpression,rangeStrings);
      if (indicator.eClass().equals(IndicatorsPackage.eINSTANCE.getModeIndicator())) {
        String genericSQL=sqlGenericExpression.getBody();
        int beginIndex=genericSQL.indexOf(dbms().orderBy());
        if (beginIndex != -1) {
          int lastIndex=genericSQL.lastIndexOf(dbms().desc());
          String orderByClause=genericSQL.substring(beginIndex,lastIndex);
          completedSqlString=completedSqlString + orderByClause + dbms().desc();
        }
        completedSqlString=dbms().getTopNQuery(completedSqlString,topN);
      }
    }
 else     if (dateAggregationType != null) {
      completedSqlString=getDateAggregatedCompletedString(sqlGenericExpression,colName,table,dateAggregationType);
      completedSqlString=addWhereToSqlStringStatement(whereExpression,completedSqlString);
      completedSqlString=dbms().getTopNQuery(completedSqlString,topN);
    }
 else {
      completedSqlString=replaceVariables(sqlGenericExpression.getBody(),colName,table);
      completedSqlString=addWhereToSqlStringStatement(whereExpression,completedSqlString);
      completedSqlString=dbms().getTopNQuery(completedSqlString,topN);
    }
  }
 else   if (indicator.eClass().equals(IndicatorsPackage.eINSTANCE.getUniqueCountIndicator())) {
    completedSqlString=replaceVariables(sqlGenericExpression.getBody(),colName,table);
    completedSqlString=addWhereToSqlStringStatement(whereExpression,completedSqlString);
    completedSqlString=dbms().countRowInSubquery(completedSqlString,"String_Node_Str");
  }
 else   if (indicator.eClass().equals(IndicatorsPackage.eINSTANCE.getDuplicateCountIndicator())) {
    completedSqlString=replaceVariables(sqlGenericExpression.getBody(),colName,table);
    completedSqlString=addWhereToSqlStringStatement(whereExpression,completedSqlString);
    completedSqlString=dbms().countRowInSubquery(completedSqlString,"String_Node_Str");
  }
 else   if (indicator.eClass().equals(IndicatorsPackage.eINSTANCE.getPatternMatchingIndicator())) {
    List<String> patterns=getPatterns(indicator);
    if (patterns.isEmpty()) {
      return traceError("String_Node_Str" + language + "String_Node_Str"+ indicator.getName());
    }
    completedSqlString=replaceVariables(sqlGenericExpression.getBody(),colName,table,patterns);
    completedSqlString=addWhereToSqlStringStatement(whereExpression,completedSqlString);
  }
 else {
    completedSqlString=replaceVariables(sqlGenericExpression.getBody(),colName,table);
    completedSqlString=addWhereToSqlStringStatement(whereExpression,completedSqlString);
  }
  if (log.isDebugEnabled()) {
    log.debug("String_Node_Str" + language + "String_Node_Str"+ completedSqlString);
  }
  String finalQuery=completedSqlString;
  Expression instantiateSqlExpression=instantiateSqlExpression(language,finalQuery);
  indicator.setInstantiatedExpression(instantiateSqlExpression);
  return true;
}

Fixed version (ground truth):
/** 
 * DOC scorreia Comment method "createSqlQuery".
 * @param dataFilterExpression
 * @param analysis
 * @param indicator
 * @throws ParseException
 * @throws AnalysisExecutionException
 */
private boolean createSqlQuery(String dataFilterAsString,Indicator indicator) throws ParseException, AnalysisExecutionException {
  ModelElement analyzedElement=indicator.getAnalyzedElement();
  if (analyzedElement == null) {
    return traceError("String_Node_Str" + indicator.getName());
  }
  TdColumn tdColumn=SwitchHelpers.COLUMN_SWITCH.doSwitch(indicator.getAnalyzedElement());
  if (tdColumn == null) {
    return traceError("String_Node_Str" + indicator.getName());
  }
  String colName=quote(tdColumn.getName());
  if (!belongToSameSchemata(tdColumn)) {
    StringBuffer buf=new StringBuffer();
    for (    orgomg.cwm.objectmodel.core.Package schema : schemata.values()) {
      buf.append(schema.getName() + "String_Node_Str");
    }
    log.error("String_Node_Str" + colName + "String_Node_Str"+ buf.toString().trim()+ "String_Node_Str");
    return false;
  }
  colName=castColumn(indicator,tdColumn,colName);
  String language=dbms().getDbmsName();
  Expression sqlGenericExpression=null;
  IndicatorDefinition indicatorDefinition=indicator.getIndicatorDefinition();
  if (indicatorDefinition == null) {
    return traceError("String_Node_Str" + indicator.getName());
  }
  sqlGenericExpression=getSqlExpression(indicatorDefinition,language);
  if (sqlGenericExpression == null) {
    log.warn("String_Node_Str" + language + "String_Node_Str"+ indicatorDefinition.getName());
    if (log.isInfoEnabled()) {
      log.info("String_Node_Str" + dbms().getDefaultLanguage());
    }
    sqlGenericExpression=getSqlExpression(indicatorDefinition,dbms().getDefaultLanguage());
  }
  if (sqlGenericExpression == null || sqlGenericExpression.getBody() == null) {
    return traceError("String_Node_Str" + (indicator.getName() != null ? indicator.getName() : indicator.eClass().getName()) + "String_Node_Str"+ ResourceHelper.getUUID(indicatorDefinition));
  }
  List<String> whereExpression=new ArrayList<String>();
  if (StringUtils.isNotBlank(dataFilterAsString)) {
    whereExpression.add(dataFilterAsString);
  }
  List<String> rangeStrings=null;
  DateGrain dateAggregationType=null;
  IndicatorParameters parameters=indicator.getParameters();
  if (parameters != null) {
    Domain bins=parameters.getBins();
    if (bins != null) {
      rangeStrings=getBinsAsGenericString(bins.getRanges());
    }
    DateParameters dateParameters=parameters.getDateParameters();
    if (dateParameters != null) {
      dateAggregationType=dateParameters.getDateAggregationType();
    }
    TextParameters textParameter=parameters.getTextParameter();
    if (textParameter != null) {
      if (textParameter.isIgnoreCase()) {
        colName=dbms().toUpperCase(colName);
      }
      if (!textParameter.isUseBlank()) {
        whereExpression.add(dbms().isNotBlank(colName));
      }
      if (textParameter.isUseNulls()) {
        colName=dbms().replaceNullsWithString(colName,"String_Node_Str");
      }
    }
  }
  String table=quote(ColumnHelper.getColumnSetFullName(tdColumn));
  String catalogName=getCatalogName(tdColumn);
  table=dbms().toQualifiedName(catalogName,null,table);
  String completedSqlString=null;
  if (indicator.eClass().equals(IndicatorsPackage.eINSTANCE.getMedianIndicator()) || indicator.eClass().equals(IndicatorsPackage.eINSTANCE.getLowerQuartileIndicator()) || indicator.eClass().equals(IndicatorsPackage.eINSTANCE.getUpperQuartileIndicator())) {
    completedSqlString=getCompletedStringForQuantiles(indicator,sqlGenericExpression,colName,table,whereExpression);
    completedSqlString=addWhereToSqlStringStatement(whereExpression,completedSqlString);
  }
 else   if (indicator.eClass().equals(IndicatorsPackage.eINSTANCE.getFrequencyIndicator()) || indicator.eClass().equals(IndicatorsPackage.eINSTANCE.getModeIndicator())) {
    int topN=indicator.eClass().equals(IndicatorsPackage.eINSTANCE.getModeIndicator()) ? 1 : getTopN(indicator);
    if (topN <= 0) {
      topN=TOP_N;
    }
    if (rangeStrings != null) {
      completedSqlString=getUnionCompletedString(indicator,sqlGenericExpression,colName,table,whereExpression,rangeStrings);
      if (indicator.eClass().equals(IndicatorsPackage.eINSTANCE.getModeIndicator())) {
        String genericSQL=sqlGenericExpression.getBody();
        int beginIndex=genericSQL.indexOf(dbms().orderBy());
        if (beginIndex != -1) {
          int lastIndex=genericSQL.lastIndexOf(dbms().desc());
          String orderByClause=genericSQL.substring(beginIndex,lastIndex);
          completedSqlString=completedSqlString + orderByClause + dbms().desc();
        }
        completedSqlString=dbms().getTopNQuery(completedSqlString,topN);
      }
    }
 else     if ((dateAggregationType != null) && Java2SqlType.isDateInSQL(tdColumn.getJavaType())) {
      completedSqlString=getDateAggregatedCompletedString(sqlGenericExpression,colName,table,dateAggregationType);
      completedSqlString=addWhereToSqlStringStatement(whereExpression,completedSqlString);
      completedSqlString=dbms().getTopNQuery(completedSqlString,topN);
    }
 else {
      completedSqlString=replaceVariables(sqlGenericExpression.getBody(),colName,table);
      completedSqlString=addWhereToSqlStringStatement(whereExpression,completedSqlString);
      completedSqlString=dbms().getTopNQuery(completedSqlString,topN);
    }
  }
 else   if (indicator.eClass().equals(IndicatorsPackage.eINSTANCE.getUniqueCountIndicator())) {
    completedSqlString=replaceVariables(sqlGenericExpression.getBody(),colName,table);
    completedSqlString=addWhereToSqlStringStatement(whereExpression,completedSqlString);
    completedSqlString=dbms().countRowInSubquery(completedSqlString,"String_Node_Str");
  }
 else   if (indicator.eClass().equals(IndicatorsPackage.eINSTANCE.getDuplicateCountIndicator())) {
    completedSqlString=replaceVariables(sqlGenericExpression.getBody(),colName,table);
    completedSqlString=addWhereToSqlStringStatement(whereExpression,completedSqlString);
    completedSqlString=dbms().countRowInSubquery(completedSqlString,"String_Node_Str");
  }
 else   if (indicator.eClass().equals(IndicatorsPackage.eINSTANCE.getPatternMatchingIndicator())) {
    List<String> patterns=getPatterns(indicator);
    if (patterns.isEmpty()) {
      return traceError("String_Node_Str" + language + "String_Node_Str"+ indicator.getName());
    }
    completedSqlString=replaceVariables(sqlGenericExpression.getBody(),colName,table,patterns);
    completedSqlString=addWhereToSqlStringStatement(whereExpression,completedSqlString);
  }
 else {
    completedSqlString=replaceVariables(sqlGenericExpression.getBody(),colName,table);
    completedSqlString=addWhereToSqlStringStatement(whereExpression,completedSqlString);
  }
  if (log.isDebugEnabled()) {
    log.debug("String_Node_Str" + language + "String_Node_Str"+ completedSqlString);
  }
  String finalQuery=completedSqlString;
  Expression instantiateSqlExpression=instantiateSqlExpression(language,finalQuery);
  indicator.setInstantiatedExpression(instantiateSqlExpression);
  return true;
}