Program: 138224
Project: 138224
Buggy source:
/** 
 * 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(Messages.getString("String_Node_Str",indicator.getName()));
  }
  TdColumn tdColumn=SwitchHelpers.COLUMN_SWITCH.doSwitch(indicator.getAnalyzedElement());
  if (tdColumn == null) {
    return traceError(Messages.getString("String_Node_Str",indicator.getName()));
  }
  if (tdColumn.eIsProxy()) {
    tdColumn=(TdColumn)EObjectHelper.resolveObject(tdColumn);
  }
  String colName=getQuotedColumnName(tdColumn);
  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(Messages.getString("String_Node_Str",colName,buf.toString().trim()));
    return false;
  }
  colName=castColumn(indicator,tdColumn,colName);
  String language=dbms().getDbmsName();
  Expression sqlGenericExpression=null;
  IndicatorDefinition indicatorDefinition;
  String label=indicator.getIndicatorDefinition().getLabel();
  if (label == null || PluginConstant.EMPTY_STRING.equals(label)) {
    indicatorDefinition=indicator.getIndicatorDefinition();
  }
 else {
    indicatorDefinition=DefinitionHandler.getInstance().getIndicatorDefinition(label);
  }
  if (indicatorDefinition == null) {
    return traceError(Messages.getString("String_Node_Str",indicator.getName()));
  }
  sqlGenericExpression=dbms().getSqlExpression(indicatorDefinition);
  final EClass indicatorEclass=indicator.eClass();
  if (sqlGenericExpression == null || sqlGenericExpression.getBody() == null) {
    if (IndicatorsPackage.eINSTANCE.getRegexpMatchingIndicator().equals(indicatorEclass)) {
      return traceError(Messages.getString("String_Node_Str",language));
    }
    traceError(Messages.getString("String_Node_Str",new Object[]{(indicator.getName() != null ? indicator.getName() : indicatorEclass.getName()),ResourceHelper.getUUID(indicatorDefinition),language}));
  }
  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(),colName);
    }
    DateParameters dateParameters=parameters.getDateParameters();
    if (dateParameters != null) {
      dateAggregationType=dateParameters.getDateAggregationType();
    }
    TextParameters textParameter=parameters.getTextParameter();
    if (textParameter != null) {
      if (textParameter.isUseNulls()) {
        colName=dbms().replaceNullsWithString(colName,"String_Node_Str");
      }
 else {
        whereExpression.add(colName.concat(dbms().isNotNull()));
      }
      if (textParameter.isIgnoreCase()) {
        colName=dbms().toUpperCase(colName);
      }
      if (!textParameter.isUseBlank() && IndicatorsPackage.eINSTANCE.getLengthIndicator().isSuperTypeOf(indicatorEclass)) {
        String tdColName=getQuotedColumnName(tdColumn);
        tdColName=dbms().replaceNullsWithString(tdColName,"String_Node_Str");
        whereExpression.add(dbms().isNotBlank(tdColName));
      }
 else       if (textParameter.isUseBlank() && IndicatorsPackage.eINSTANCE.getFrequencyIndicator().isSuperTypeOf(indicatorEclass)) {
        colName=dbms().trim(colName);
      }
    }
  }
  final ColumnSet columnSetOwner=ColumnHelper.getColumnOwnerAsColumnSet(tdColumn);
  String schemaName=getQuotedSchemaName(columnSetOwner);
  String table=getQuotedTableName(tdColumn);
  String catalogName=getQuotedCatalogName(columnSetOwner);
  if (catalogName == null && schemaName != null) {
    final Schema parentSchema=SchemaHelper.getParentSchema(columnSetOwner);
    final Catalog parentCatalog=CatalogHelper.getParentCatalog(parentSchema);
    catalogName=parentCatalog != null ? parentCatalog.getName() : null;
  }
  boolean isSybase=false;
  if (Arrays.asList(org.talend.utils.sql.ConnectionUtils.getSybaseDBProductsName()).contains(dbms().getDbmsName())) {
    isSybase=true;
  }
  if (isSybase) {
    schemaName=ColumnSetHelper.getTableOwner(columnSetOwner);
  }
  table=dbms().toQualifiedName(catalogName,schemaName,table);
  String completedSqlString=null;
  if (indicatorEclass.equals(IndicatorsPackage.eINSTANCE.getMedianIndicator()) || indicatorEclass.equals(IndicatorsPackage.eINSTANCE.getLowerQuartileIndicator()) || indicatorEclass.equals(IndicatorsPackage.eINSTANCE.getUpperQuartileIndicator())) {
    completedSqlString=getCompletedStringForQuantiles(indicator,sqlGenericExpression,colName,table,whereExpression);
    if (completedSqlString != null) {
      whereExpression=duplicateForCrossJoin(completedSqlString,whereExpression,tdColumn);
      completedSqlString=addWhereToSqlStringStatement(whereExpression,completedSqlString);
    }
  }
 else   if (indicatorEclass.equals(IndicatorsPackage.eINSTANCE.getFrequencyIndicator()) || IndicatorsPackage.eINSTANCE.getFrequencyIndicator().isSuperTypeOf(indicatorEclass) || indicatorEclass.equals(IndicatorsPackage.eINSTANCE.getModeIndicator())|| UDIHelper.isFrequency(indicator)) {
    int topN=indicatorEclass.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 (indicatorEclass.equals(IndicatorsPackage.eINSTANCE.getModeIndicator())) {
        completedSqlString=dbms().getTopNQuery(completedSqlString,topN);
      }
    }
 else     if (dateAggregationType != null && !dateAggregationType.equals(DateGrain.NONE) && Java2SqlType.isDateInSQL(tdColumn.getSqlDataType().getJavaDataType())) {
      completedSqlString=getDateAggregatedCompletedStringWithoutAlia(sqlGenericExpression,colName,table,dateAggregationType);
      completedSqlString=addWhereToSqlStringStatement(whereExpression,completedSqlString);
      completedSqlString=dbms().getTopNQuery(completedSqlString,topN);
    }
 else {
      if (indicatorEclass.equals(IndicatorsPackage.eINSTANCE.getPatternFreqIndicator()) || indicatorEclass.equals(IndicatorsPackage.eINSTANCE.getPatternLowFreqIndicator())) {
        if (Java2SqlType.isNumbericInSQL(tdColumn.getSqlDataType().getJavaDataType()) || Java2SqlType.isDateInSQL(tdColumn.getSqlDataType().getJavaDataType())) {
          colName=addFunctionTypeConvert(colName);
        }
        final EList<CharactersMapping> charactersMapping=indicatorDefinition.getCharactersMapping();
        colName=dbms().getPatternFinderFunction(colName,charactersMapping);
        if (colName == null) {
          colName=dbms().getPatternFinderDefaultFunction(colName);
        }
        if (colName == null) {
          return traceError(Messages.getString("String_Node_Str",language,indicator.getName()));
        }
      }
 else       if (indicatorEclass.equals(IndicatorsPackage.eINSTANCE.getSoundexFreqIndicator()) || indicatorEclass.equals(IndicatorsPackage.eINSTANCE.getSoundexLowFreqIndicator())) {
        table=dbms().getSoundexFunction(table,colName);
      }
      completedSqlString=dbms().fillGenericQueryWithColumnTableAndAlias(sqlGenericExpression.getBody(),colName,table,colName);
      completedSqlString=addWhereToSqlStringStatement(whereExpression,completedSqlString);
      completedSqlString=dbms().getTopNQuery(completedSqlString,topN);
    }
  }
 else   if (IndicatorsPackage.eINSTANCE.getPatternMatchingIndicator().isSuperTypeOf(indicatorEclass)) {
    List<String> patterns=getPatterns(indicator);
    if (patterns.isEmpty()) {
      return traceError(Messages.getString("String_Node_Str",language,indicator.getName()));
    }
    completedSqlString=replaceVariables(sqlGenericExpression.getBody(),colName,table,patterns);
    completedSqlString=addWhereToSqlStringStatement(whereExpression,completedSqlString);
  }
 else {
    if (IndicatorsPackage.eINSTANCE.getDefValueCountIndicator().equals(indicatorEclass)) {
      String defValue=ColumnHelper.getDefaultValue(tdColumn);
      if (defValue == null) {
        return traceError(Messages.getString("String_Node_Str",colName,indicator.getName()));
      }
      int javaType=tdColumn.getSqlDataType().getJavaDataType();
      if (!Java2SqlType.isNumbericInSQL(javaType) && !isFunction(defValue,table)) {
        defValue="String_Node_Str" + defValue + "String_Node_Str";
      }
      whereExpression.add(colName + dbms().equal() + defValue);
    }
    completedSqlString=dbms().fillGenericQueryWithColumnsAndTable(sqlGenericExpression.getBody(),colName,table);
    completedSqlString=addWhereToSqlStringStatement(whereExpression,completedSqlString);
  }
  String finalQuery=completedSqlString;
  if (finalQuery != null) {
    TdExpression instantiateSqlExpression=BooleanExpressionHelper.createTdExpression(language,finalQuery);
    indicator.setInstantiatedExpression(instantiateSqlExpression);
    return true;
  }
  return false;
}

Fixed version (ground truth):
/** 
 * 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(Messages.getString("String_Node_Str",indicator.getName()));
  }
  TdColumn tdColumn=SwitchHelpers.COLUMN_SWITCH.doSwitch(indicator.getAnalyzedElement());
  if (tdColumn == null) {
    return traceError(Messages.getString("String_Node_Str",indicator.getName()));
  }
  if (tdColumn.eIsProxy()) {
    tdColumn=(TdColumn)EObjectHelper.resolveObject(tdColumn);
  }
  String colName=getQuotedColumnName(tdColumn);
  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(Messages.getString("String_Node_Str",colName,buf.toString().trim()));
    return false;
  }
  colName=castColumn(indicator,tdColumn,colName);
  String language=dbms().getDbmsName();
  Expression sqlGenericExpression=null;
  IndicatorDefinition indicatorDefinition;
  String label=indicator.getIndicatorDefinition().getLabel();
  if (label == null || PluginConstant.EMPTY_STRING.equals(label)) {
    indicatorDefinition=indicator.getIndicatorDefinition();
  }
 else {
    indicatorDefinition=DefinitionHandler.getInstance().getIndicatorDefinition(label);
  }
  if (indicatorDefinition == null) {
    return traceError(Messages.getString("String_Node_Str",indicator.getName()));
  }
  sqlGenericExpression=dbms().getSqlExpression(indicatorDefinition);
  final EClass indicatorEclass=indicator.eClass();
  if (sqlGenericExpression == null || sqlGenericExpression.getBody() == null) {
    if (IndicatorsPackage.eINSTANCE.getRegexpMatchingIndicator().equals(indicatorEclass)) {
      return traceError(Messages.getString("String_Node_Str",language));
    }
    traceError(Messages.getString("String_Node_Str",new Object[]{(indicator.getName() != null ? indicator.getName() : indicatorEclass.getName()),ResourceHelper.getUUID(indicatorDefinition),language}));
  }
  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(),colName);
    }
    DateParameters dateParameters=parameters.getDateParameters();
    if (dateParameters != null) {
      dateAggregationType=dateParameters.getDateAggregationType();
    }
    TextParameters textParameter=parameters.getTextParameter();
    if (textParameter != null) {
      if (textParameter.isUseNulls()) {
        colName=dbms().replaceNullsWithString(colName,"String_Node_Str");
      }
 else {
        whereExpression.add(colName.concat(dbms().isNotNull()));
      }
      if (textParameter.isIgnoreCase()) {
        colName=dbms().toUpperCase(colName);
      }
      if (!textParameter.isUseBlank() && IndicatorsPackage.eINSTANCE.getLengthIndicator().isSuperTypeOf(indicatorEclass)) {
        String tdColName=getQuotedColumnName(tdColumn);
        tdColName=dbms().replaceNullsWithString(tdColName,"String_Node_Str");
        whereExpression.add(dbms().isNotBlank(tdColName));
      }
 else       if (textParameter.isUseBlank() && IndicatorsPackage.eINSTANCE.getFrequencyIndicator().isSuperTypeOf(indicatorEclass)) {
        colName=dbms().trim(colName);
      }
    }
  }
  final ColumnSet columnSetOwner=ColumnHelper.getColumnOwnerAsColumnSet(tdColumn);
  String schemaName=getQuotedSchemaName(columnSetOwner);
  String table=getQuotedTableName(tdColumn);
  String catalogName=getQuotedCatalogName(columnSetOwner);
  if (catalogName == null && schemaName != null) {
    final Schema parentSchema=SchemaHelper.getParentSchema(columnSetOwner);
    final Catalog parentCatalog=CatalogHelper.getParentCatalog(parentSchema);
    catalogName=parentCatalog != null ? parentCatalog.getName() : null;
  }
  boolean isSybase=false;
  if (Arrays.asList(org.talend.utils.sql.ConnectionUtils.getSybaseDBProductsName()).contains(dbms().getDbmsName())) {
    isSybase=true;
  }
  if (isSybase) {
    schemaName=ColumnSetHelper.getTableOwner(columnSetOwner);
  }
  table=dbms().toQualifiedName(catalogName,schemaName,table);
  String completedSqlString=null;
  if (indicatorEclass.equals(IndicatorsPackage.eINSTANCE.getMedianIndicator()) || indicatorEclass.equals(IndicatorsPackage.eINSTANCE.getLowerQuartileIndicator()) || indicatorEclass.equals(IndicatorsPackage.eINSTANCE.getUpperQuartileIndicator())) {
    completedSqlString=getCompletedStringForQuantiles(indicator,sqlGenericExpression,colName,table,whereExpression);
    if (completedSqlString != null) {
      whereExpression=duplicateForCrossJoin(completedSqlString,whereExpression,tdColumn);
      completedSqlString=addWhereToSqlStringStatement(whereExpression,completedSqlString);
    }
  }
 else   if (indicatorEclass.equals(IndicatorsPackage.eINSTANCE.getFrequencyIndicator()) || IndicatorsPackage.eINSTANCE.getFrequencyIndicator().isSuperTypeOf(indicatorEclass) || indicatorEclass.equals(IndicatorsPackage.eINSTANCE.getModeIndicator())|| UDIHelper.isFrequency(indicator)) {
    int topN=indicatorEclass.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 (indicatorEclass.equals(IndicatorsPackage.eINSTANCE.getModeIndicator())) {
        completedSqlString=dbms().getTopNQuery(completedSqlString,topN);
      }
    }
 else     if (dateAggregationType != null && !dateAggregationType.equals(DateGrain.NONE) && Java2SqlType.isDateInSQL(tdColumn.getSqlDataType().getJavaDataType())) {
      completedSqlString=getDateAggregatedCompletedStringWithoutAlia(sqlGenericExpression,colName,table,dateAggregationType);
      completedSqlString=addWhereToSqlStringStatement(whereExpression,completedSqlString);
      completedSqlString=dbms().getTopNQuery(completedSqlString,topN);
    }
 else {
      if (indicatorEclass.equals(IndicatorsPackage.eINSTANCE.getPatternFreqIndicator()) || indicatorEclass.equals(IndicatorsPackage.eINSTANCE.getPatternLowFreqIndicator())) {
        if (Java2SqlType.isNumbericInSQL(tdColumn.getSqlDataType().getJavaDataType()) || Java2SqlType.isDateInSQL(tdColumn.getSqlDataType().getJavaDataType())) {
          colName=addFunctionTypeConvert(colName);
        }
        final EList<CharactersMapping> charactersMapping=indicatorDefinition.getCharactersMapping();
        colName=dbms().getPatternFinderFunction(colName,charactersMapping);
        if (colName == null) {
          colName=dbms().getPatternFinderDefaultFunction(colName);
        }
        if (colName == null) {
          return traceError(Messages.getString("String_Node_Str",language,indicator.getName()));
        }
      }
 else       if (indicatorEclass.equals(IndicatorsPackage.eINSTANCE.getSoundexFreqIndicator()) || indicatorEclass.equals(IndicatorsPackage.eINSTANCE.getSoundexLowFreqIndicator())) {
        table=dbms().getSoundexFunction(table,colName);
      }
      completedSqlString=dbms().fillGenericQueryWithColumnTableAndAlias(sqlGenericExpression.getBody(),colName,table,colName);
      completedSqlString=addWhereToSqlStringStatement(whereExpression,completedSqlString);
      completedSqlString=dbms().getTopNQuery(completedSqlString,topN);
    }
  }
 else   if (IndicatorsPackage.eINSTANCE.getPatternMatchingIndicator().isSuperTypeOf(indicatorEclass)) {
    List<String> patterns=getPatterns(indicator);
    if (patterns.isEmpty()) {
      return traceError(Messages.getString("String_Node_Str",language,indicator.getName()));
    }
    completedSqlString=replaceVariables(sqlGenericExpression.getBody(),colName,table,patterns);
    completedSqlString=addWhereToSqlStringStatement(whereExpression,completedSqlString);
  }
 else {
    if (IndicatorsPackage.eINSTANCE.getDefValueCountIndicator().equals(indicatorEclass)) {
      String defValue=ColumnHelper.getDefaultValue(tdColumn);
      if (defValue == null) {
        return traceError(Messages.getString("String_Node_Str",colName,indicator.getName()));
      }
      int javaType=tdColumn.getSqlDataType().getJavaDataType();
      if (!Java2SqlType.isNumbericInSQL(javaType) && !isFunction(defValue,table)) {
        defValue="String_Node_Str" + defValue + "String_Node_Str";
      }
      whereExpression.add(colName + dbms().equal() + defValue);
    }
    String sqlBody=sqlGenericExpression.getBody();
    if (sqlBody.indexOf("String_Node_Str") != -1) {
      completedSqlString=dbms().fillGenericQueryWithColumnTableAndAlias(sqlBody,colName,table,colName);
    }
 else {
      completedSqlString=dbms().fillGenericQueryWithColumnsAndTable(sqlBody,colName,table);
    }
    completedSqlString=addWhereToSqlStringStatement(whereExpression,completedSqlString);
  }
  String finalQuery=completedSqlString;
  if (finalQuery != null) {
    TdExpression instantiateSqlExpression=BooleanExpressionHelper.createTdExpression(language,finalQuery);
    indicator.setInstantiatedExpression(instantiateSqlExpression);
    return true;
  }
  return false;
}