Program: 68641
Project: 68641
Buggy source:
private void addSorting(ICubeQueryDefinition cubeQuery,CubeHandle cube,SeriesDefinition sd,int i) throws BirtException {
  if (sd.getSortKey() == null) {
    return;
  }
  String sortKey=sd.getSortKey().getDefinition();
  if (sd.isSetSorting() && sortKey != null && sortKey.length() > 0) {
    String sortKeyBinding=ChartExpressionUtil.getCubeBindingName(sd.getSortKey().getDefinition(),true);
    if (registeredLevels.containsKey(sortKeyBinding)) {
      ICubeSortDefinition sortDef=getCubeElementFactory().createCubeSortDefinition(sortKey,registeredLevels.get(sortKeyBinding),null,null,sd.getSorting() == SortOption.ASCENDING_LITERAL ? ISortDefinition.SORT_ASC : ISortDefinition.SORT_DESC);
      cubeQuery.addSort(sortDef);
    }
 else     if (registeredMeasures.containsKey(sortKeyBinding)) {
      Query targetQuery=i > 0 ? sd.getQuery() : (Query)sd.getDesignTimeSeries().getDataDefinition().get(0);
      IMeasureDefinition mDef=registeredMeasures.get(sortKeyBinding);
      String targetBindingName=ChartExpressionUtil.getCubeBindingName(targetQuery.getDefinition(),true);
      IBinding measureBinding=registeredBindings.get(sortKeyBinding);
      IBinding aggBinding=new Binding(measureBinding.getBindingName() + targetBindingName);
      aggBinding.setDataType(measureBinding.getDataType());
      aggBinding.setExpression(measureBinding.getExpression());
      aggBinding.addAggregateOn(registeredQueries.get(targetBindingName));
      aggBinding.setAggrFunction(mDef.getAggrFunction());
      aggBinding.setExportable(false);
      cubeQuery.addBinding(aggBinding);
      ICubeSortDefinition sortDef=getCubeElementFactory().createCubeSortDefinition(ExpressionUtil.createJSDataExpression(aggBinding.getBindingName()),registeredLevels.get(targetBindingName),null,null,sd.getSorting() == SortOption.ASCENDING_LITERAL ? ISortDefinition.SORT_ASC : ISortDefinition.SORT_DESC);
      cubeQuery.addSort(sortDef);
    }
  }
}

Fixed version (ground truth):
private void addSorting(ICubeQueryDefinition cubeQuery,CubeHandle cube,SeriesDefinition sd,int i) throws BirtException {
  if (sd.getSortKey() == null) {
    return;
  }
  String sortKey=sd.getSortKey().getDefinition();
  if (sd.isSetSorting() && sortKey != null && sortKey.length() > 0) {
    String sortKeyBinding=ChartExpressionUtil.getCubeBindingName(sd.getSortKey().getDefinition(),true);
    if (registeredLevels.containsKey(sortKeyBinding)) {
      ICubeSortDefinition sortDef=getCubeElementFactory().createCubeSortDefinition(sortKey.trim(),registeredLevels.get(sortKeyBinding),null,null,sd.getSorting() == SortOption.ASCENDING_LITERAL ? ISortDefinition.SORT_ASC : ISortDefinition.SORT_DESC);
      cubeQuery.addSort(sortDef);
    }
 else     if (registeredMeasures.containsKey(sortKeyBinding)) {
      Query targetQuery=i > 0 ? sd.getQuery() : (Query)sd.getDesignTimeSeries().getDataDefinition().get(0);
      IMeasureDefinition mDef=registeredMeasures.get(sortKeyBinding);
      String targetBindingName=ChartExpressionUtil.getCubeBindingName(targetQuery.getDefinition(),true);
      IBinding measureBinding=registeredBindings.get(sortKeyBinding);
      IBinding aggBinding=new Binding(measureBinding.getBindingName() + targetBindingName);
      aggBinding.setDataType(measureBinding.getDataType());
      aggBinding.setExpression(measureBinding.getExpression());
      aggBinding.addAggregateOn(registeredQueries.get(targetBindingName));
      aggBinding.setAggrFunction(mDef.getAggrFunction());
      aggBinding.setExportable(false);
      cubeQuery.addBinding(aggBinding);
      ICubeSortDefinition sortDef=getCubeElementFactory().createCubeSortDefinition(ExpressionUtil.createJSDataExpression(aggBinding.getBindingName()),registeredLevels.get(targetBindingName),null,null,sd.getSorting() == SortOption.ASCENDING_LITERAL ? ISortDefinition.SORT_ASC : ISortDefinition.SORT_DESC);
      cubeQuery.addSort(sortDef);
    }
  }
}