Program: 68366
Project: 68366
Buggy source:
private static ICubeQueryDefinition cloneCubeQueryDefinition(ICubeQueryDefinition query,IEdgeDrillFilter columnDrill,IEdgeDrillFilter rowDrill,ICube cube) throws DataException {
  ICubeQueryDefinition cloneQuery=null;
  if (query != null) {
    cloneQuery=new DrillCubeQueryDefinition(query.getName());
    if (query.getEdge(ICubeQueryDefinition.COLUMN_EDGE) != null) {
      if (columnDrill != null)       ((DrillCubeQueryDefinition)cloneQuery).setTupleOnColumn(columnDrill.getTuple());
      cloneEdgeDefinition(cloneQuery,query.getEdge(ICubeQueryDefinition.COLUMN_EDGE),ICubeQueryDefinition.COLUMN_EDGE,columnDrill,cube);
    }
    if (query.getEdge(ICubeQueryDefinition.ROW_EDGE) != null) {
      if (rowDrill != null)       ((DrillCubeQueryDefinition)cloneQuery).setTupleOnRow(rowDrill.getTuple());
      cloneEdgeDefinition(cloneQuery,query.getEdge(ICubeQueryDefinition.ROW_EDGE),ICubeQueryDefinition.ROW_EDGE,rowDrill,cube);
    }
    for (int i=0; i < query.getMeasures().size(); i++) {
      IMeasureDefinition measure=((IMeasureDefinition)query.getMeasures().get(i));
      IMeasureDefinition cloneMeasure=cloneQuery.createMeasure(measure.getName());
      cloneMeasure.setAggrFunction(measure.getAggrFunction());
    }
    for (int i=0; i < query.getComputedMeasures().size(); i++) {
      IComputedMeasureDefinition measure=((IComputedMeasureDefinition)query.getComputedMeasures().get(i));
      IMeasureDefinition cloneMeasure=cloneQuery.createComputedMeasure(measure.getName(),measure.getType(),measure.getExpression());
      cloneMeasure.setAggrFunction(measure.getAggrFunction());
    }
    List<String> levelDefnOnColumn=new ArrayList<String>();
    List<String> levelDefnOnRow=new ArrayList<String>();
    if (columnDrill != null) {
      levelDefnOnColumn=findLevelExpressionFromQuery(query.getEdge(ICubeQueryDefinition.COLUMN_EDGE),columnDrill,cube);
    }
    if (rowDrill != null) {
      levelDefnOnRow=findLevelExpressionFromQuery(query.getEdge(ICubeQueryDefinition.ROW_EDGE),rowDrill,cube);
    }
    for (int i=0; i < query.getBindings().size(); i++) {
      IBinding binding=(IBinding)query.getBindings().get(i);
      if (binding.getAggrFunction() != null) {
        List dimLevelOnColumn=getReferenceDimLevelOnEdge(query,ICubeQueryDefinition.COLUMN_EDGE);
        List dimLevelOnRow=getReferenceDimLevelOnEdge(query,ICubeQueryDefinition.ROW_EDGE);
        IBinding newBinding=new Binding(binding.getBindingName(),binding.getExpression());
        newBinding.setDataType(binding.getDataType());
        newBinding.setAggrFunction(binding.getAggrFunction());
        newBinding.setDisplayName(binding.getDisplayName());
        newBinding.setExportable(binding.exportable());
        newBinding.setFilter(newBinding.getFilter());
        for (int k=0; k < binding.getArguments().size(); k++) {
          newBinding.addArgument((IBaseExpression)binding.getArguments().get(k));
        }
        List aggrOns=binding.getAggregatOns();
        if (aggrOns != null && !aggrOns.isEmpty()) {
          boolean columnExist=false, rowExist=false;
          boolean detailLevelOnColumn=isGrandTotalOnEdge(aggrOns,dimLevelOnColumn);
          boolean detailLevelOnRow=isGrandTotalOnEdge(aggrOns,dimLevelOnRow);
          if (!detailLevelOnColumn && !detailLevelOnRow) {
            continue;
          }
          if ((detailLevelOnColumn && columnDrill != null) || (detailLevelOnRow && rowDrill != null)) {
            for (int k=0; k < aggrOns.size(); k++) {
              String aggrExpr=aggrOns.get(k).toString();
              DimLevel target=OlapExpressionUtil.getTargetDimLevel(aggrExpr);
              String dimensionNameOnColumn=columnDrill != null ? columnDrill.getTargetHierarchy().getDimension().getName() : null;
              String dimensionNameOnRow=rowDrill != null ? rowDrill.getTargetHierarchy().getDimension().getName() : null;
              if (!columnExist && target.getDimensionName().equals(dimensionNameOnColumn)) {
                if (!detailLevelOnColumn) {
                  newBinding.addAggregateOn(aggrExpr);
                }
 else {
                  columnExist=true;
                  for (int t=0; t < levelDefnOnColumn.size(); t++)                   newBinding.addAggregateOn((String)levelDefnOnColumn.get(t));
                }
              }
 else               if (!rowExist && target.getDimensionName().equals(dimensionNameOnRow)) {
                if (!detailLevelOnRow) {
                  newBinding.addAggregateOn(aggrExpr);
                }
 else {
                  rowExist=true;
                  for (int t=0; t < levelDefnOnRow.size(); t++)                   newBinding.addAggregateOn((String)levelDefnOnRow.get(t));
                }
              }
 else               if (!target.getDimensionName().equals(dimensionNameOnColumn) && !target.getDimensionName().equals(dimensionNameOnRow))               newBinding.addAggregateOn(aggrExpr);
            }
            cloneQuery.addBinding(newBinding);
          }
        }
      }
 else       cloneQuery.addBinding((IBinding)query.getBindings().get(i));
    }
    for (int i=0; i < query.getFilters().size(); i++) {
      cloneQuery.addFilter((IFilterDefinition)query.getFilters().get(i));
    }
    for (int i=0; i < query.getSorts().size(); i++) {
      cloneQuery.addSort((ISortDefinition)query.getSorts().get(i));
    }
  }
  return cloneQuery;
}

Fixed version (ground truth):
private static ICubeQueryDefinition cloneCubeQueryDefinition(ICubeQueryDefinition query,IEdgeDrillFilter columnDrill,IEdgeDrillFilter rowDrill,ICube cube) throws DataException {
  DrillCubeQueryDefinition cloneQuery=null;
  if (query != null) {
    cloneQuery=new DrillCubeQueryDefinition(query.getName());
    if (query.getEdge(ICubeQueryDefinition.COLUMN_EDGE) != null) {
      if (columnDrill != null) {
        ((DrillCubeQueryDefinition)cloneQuery).setTupleOnColumn(columnDrill.getTuple());
      }
      cloneEdgeDefinition(cloneQuery,query.getEdge(ICubeQueryDefinition.COLUMN_EDGE),ICubeQueryDefinition.COLUMN_EDGE,columnDrill,cube);
    }
    if (query.getEdge(ICubeQueryDefinition.ROW_EDGE) != null) {
      if (rowDrill != null) {
        ((DrillCubeQueryDefinition)cloneQuery).setTupleOnRow(rowDrill.getTuple());
      }
      cloneEdgeDefinition(cloneQuery,query.getEdge(ICubeQueryDefinition.ROW_EDGE),ICubeQueryDefinition.ROW_EDGE,rowDrill,cube);
    }
    for (int i=0; i < query.getMeasures().size(); i++) {
      IMeasureDefinition measure=((IMeasureDefinition)query.getMeasures().get(i));
      IMeasureDefinition cloneMeasure=cloneQuery.createMeasure(measure.getName());
      cloneMeasure.setAggrFunction(measure.getAggrFunction());
    }
    for (int i=0; i < query.getComputedMeasures().size(); i++) {
      IComputedMeasureDefinition measure=((IComputedMeasureDefinition)query.getComputedMeasures().get(i));
      IMeasureDefinition cloneMeasure=cloneQuery.createComputedMeasure(measure.getName(),measure.getType(),measure.getExpression());
      cloneMeasure.setAggrFunction(measure.getAggrFunction());
    }
    List<String> levelDefnOnColumn=new ArrayList<String>();
    List<String> levelDefnOnRow=new ArrayList<String>();
    if (columnDrill != null) {
      levelDefnOnColumn=findLevelExpressionFromQuery(query.getEdge(ICubeQueryDefinition.COLUMN_EDGE),columnDrill,cube);
    }
    if (rowDrill != null) {
      levelDefnOnRow=findLevelExpressionFromQuery(query.getEdge(ICubeQueryDefinition.ROW_EDGE),rowDrill,cube);
    }
    for (int i=0; i < query.getBindings().size(); i++) {
      IBinding binding=(IBinding)query.getBindings().get(i);
      if (binding.getAggrFunction() != null) {
        List dimLevelOnColumn=getReferenceDimLevelOnEdge(query,ICubeQueryDefinition.COLUMN_EDGE);
        List dimLevelOnRow=getReferenceDimLevelOnEdge(query,ICubeQueryDefinition.ROW_EDGE);
        IBinding newBinding=new Binding(binding.getBindingName(),binding.getExpression());
        newBinding.setDataType(binding.getDataType());
        newBinding.setAggrFunction(binding.getAggrFunction());
        newBinding.setDisplayName(binding.getDisplayName());
        newBinding.setExportable(binding.exportable());
        newBinding.setFilter(newBinding.getFilter());
        for (int k=0; k < binding.getArguments().size(); k++) {
          newBinding.addArgument((IBaseExpression)binding.getArguments().get(k));
        }
        List aggrOns=binding.getAggregatOns();
        if (aggrOns != null && !aggrOns.isEmpty()) {
          boolean columnExist=false, rowExist=false;
          boolean detailLevelOnColumn=isGrandTotalOnEdge(aggrOns,dimLevelOnColumn);
          boolean detailLevelOnRow=isGrandTotalOnEdge(aggrOns,dimLevelOnRow);
          if (!detailLevelOnColumn && !detailLevelOnRow) {
            continue;
          }
          if ((detailLevelOnColumn && columnDrill != null) || (detailLevelOnRow && rowDrill != null)) {
            for (int k=0; k < aggrOns.size(); k++) {
              String aggrExpr=aggrOns.get(k).toString();
              DimLevel target=OlapExpressionUtil.getTargetDimLevel(aggrExpr);
              String dimensionNameOnColumn=columnDrill != null ? columnDrill.getTargetHierarchy().getDimension().getName() : null;
              String dimensionNameOnRow=rowDrill != null ? rowDrill.getTargetHierarchy().getDimension().getName() : null;
              if (!columnExist && target.getDimensionName().equals(dimensionNameOnColumn)) {
                if (!detailLevelOnColumn) {
                  newBinding.addAggregateOn(aggrExpr);
                }
 else {
                  columnExist=true;
                  for (int t=0; t < levelDefnOnColumn.size(); t++)                   newBinding.addAggregateOn((String)levelDefnOnColumn.get(t));
                }
              }
 else               if (!rowExist && target.getDimensionName().equals(dimensionNameOnRow)) {
                if (!detailLevelOnRow) {
                  newBinding.addAggregateOn(aggrExpr);
                }
 else {
                  rowExist=true;
                  for (int t=0; t < levelDefnOnRow.size(); t++)                   newBinding.addAggregateOn((String)levelDefnOnRow.get(t));
                }
              }
 else               if (!target.getDimensionName().equals(dimensionNameOnColumn) && !target.getDimensionName().equals(dimensionNameOnRow))               newBinding.addAggregateOn(aggrExpr);
            }
            cloneQuery.addBinding(newBinding);
          }
        }
      }
 else       cloneQuery.addBinding((IBinding)query.getBindings().get(i));
    }
    for (int i=0; i < query.getFilters().size(); i++) {
      cloneQuery.addFilter((IFilterDefinition)query.getFilters().get(i));
    }
    for (int i=0; i < query.getSorts().size(); i++) {
      cloneQuery.addSort((ISortDefinition)query.getSorts().get(i));
    }
  }
  return cloneQuery;
}