Program: 66959
Project: 66959
Buggy source:
/** 
 * @param cubeQueryDefinition
 * @param cubeQueryExcutorHelper
 * @throws DataException
 */
private void populateAggregationSort(CubeQueryExecutor executor,CubeQueryExecutorHelper cubeQueryExcutorHelper,int type) throws DataException {
  List columnSort;
switch (type) {
case ICubeQueryDefinition.COLUMN_EDGE:
    columnSort=executor.getColumnEdgeSort();
  break;
case ICubeQueryDefinition.ROW_EDGE:
columnSort=executor.getRowEdgeSort();
break;
case ICubeQueryDefinition.PAGE_EDGE:
columnSort=executor.getPageEdgeSort();
default :
return;
}
for (int i=0; i < columnSort.size(); i++) {
ICubeSortDefinition cubeSort=(ICubeSortDefinition)columnSort.get(i);
ICubeQueryDefinition queryDefn=executor.getCubeQueryDefinition();
String expr=cubeSort.getExpression().getText();
ITargetSort targetSort=null;
if ((cubeSort.getAxisQualifierLevels().length == 0 && (OlapExpressionUtil.isComplexDimensionExpr(expr) || OlapExpressionUtil.isReferenceToAttribute(cubeSort.getExpression(),queryDefn.getBindings()))) || (!OlapExpressionUtil.isDirectRerenrence(cubeSort.getExpression(),executor.getCubeQueryDefinition().getBindings()))) {
Scriptable scope=executor.getSession().getSharedScope();
targetSort=new DimensionSortEvalHelper(executor.getOuterResults(),scope,queryDefn,cubeSort,executor.getSession().getEngineContext().getScriptContext());
}
 else {
String bindingName=OlapExpressionUtil.getBindingName(expr);
if (bindingName == null) continue;
List bindings=queryDefn.getBindings();
List aggrOns=null;
IBinding binding=null;
for (int j=0; j < bindings.size(); j++) {
binding=(IBinding)bindings.get(j);
if (binding.getBindingName().equals(bindingName)) {
aggrOns=binding.getAggregatOns();
break;
}
}
DimLevel[] aggrOnLevels=null;
if (aggrOns == null || aggrOns.size() == 0) {
if (binding == null) continue;
String measureName=OlapExpressionCompiler.getReferencedScriptObject(binding.getExpression(),ScriptConstants.MEASURE_SCRIPTABLE);
if (measureName == null) {
IBinding referBinding=OlapExpressionUtil.getDirectMeasureBinding(binding,bindings);
if (referBinding != null) {
measureName=OlapExpressionUtil.getMeasure(referBinding.getExpression());
bindingName=referBinding.getBindingName();
}
}
if (measureName != null) {
List measureAggrOns=CubeQueryDefinitionUtil.populateMeasureAggrOns(queryDefn);
aggrOnLevels=new DimLevel[measureAggrOns.size()];
for (int k=0; k < measureAggrOns.size(); k++) {
aggrOnLevels[k]=(DimLevel)measureAggrOns.get(k);
}
}
}
 else {
aggrOnLevels=new DimLevel[aggrOns.size()];
for (int j=0; j < aggrOnLevels.length; j++) {
aggrOnLevels[j]=OlapExpressionUtil.getTargetDimLevel(aggrOns.get(j).toString());
}
}
DimLevel[] axisLevels=new DimLevel[cubeSort.getAxisQualifierLevels().length];
for (int k=0; k < axisLevels.length; k++) {
axisLevels[k]=new DimLevel(cubeSort.getAxisQualifierLevels()[k]);
}
targetSort=new AggrSortDefinition(aggrOnLevels,bindingName,axisLevels,cubeSort.getAxisQualifierValues(),new DimLevel(cubeSort.getTargetLevel()),cubeSort.getSortDirection());
}
switch (type) {
case ICubeQueryDefinition.COLUMN_EDGE:
cubeQueryExcutorHelper.addColumnSort(targetSort);
break;
case ICubeQueryDefinition.ROW_EDGE:
cubeQueryExcutorHelper.addRowSort(targetSort);
break;
case ICubeQueryDefinition.PAGE_EDGE:
cubeQueryExcutorHelper.addPageSort(targetSort);
}
}
}

Fixed version (ground truth):
/** 
 * @param cubeQueryDefinition
 * @param cubeQueryExcutorHelper
 * @throws DataException
 */
private void populateAggregationSort(CubeQueryExecutor executor,CubeQueryExecutorHelper cubeQueryExcutorHelper,int type) throws DataException {
  List columnSort;
switch (type) {
case ICubeQueryDefinition.COLUMN_EDGE:
    columnSort=executor.getColumnEdgeSort();
  break;
case ICubeQueryDefinition.ROW_EDGE:
columnSort=executor.getRowEdgeSort();
break;
case ICubeQueryDefinition.PAGE_EDGE:
columnSort=executor.getPageEdgeSort();
default :
return;
}
for (int i=0; i < columnSort.size(); i++) {
ICubeSortDefinition cubeSort=(ICubeSortDefinition)columnSort.get(i);
ICubeQueryDefinition queryDefn=executor.getCubeQueryDefinition();
String expr=cubeSort.getExpression().getText();
ITargetSort targetSort=null;
if ((cubeSort.getAxisQualifierLevels().length == 0 && (OlapExpressionUtil.isComplexDimensionExpr(expr) || OlapExpressionUtil.isReferenceToAttribute(cubeSort.getExpression(),queryDefn.getBindings()))) || (!OlapExpressionUtil.isDirectRerenrence(cubeSort.getExpression(),executor.getCubeQueryDefinition().getBindings()))) {
Scriptable scope=executor.getSession().getSharedScope();
targetSort=new DimensionSortEvalHelper(executor.getOuterResults(),scope,queryDefn,cubeSort,executor.getSession().getEngineContext().getScriptContext());
}
 else {
String bindingName=OlapExpressionUtil.getBindingName(expr);
if (bindingName == null) continue;
List bindings=queryDefn.getBindings();
List aggrOns=null;
IBinding binding=null;
for (int j=0; j < bindings.size(); j++) {
binding=(IBinding)bindings.get(j);
if (binding.getBindingName().equals(bindingName)) {
aggrOns=binding.getAggregatOns();
break;
}
}
DimLevel[] aggrOnLevels=null;
if (aggrOns == null || aggrOns.size() == 0) {
if (binding == null) continue;
String measureName=OlapExpressionCompiler.getReferencedScriptObject(binding.getExpression(),ScriptConstants.MEASURE_SCRIPTABLE);
if (measureName == null) {
IBinding referBinding=OlapExpressionUtil.getDirectMeasureBinding(binding,bindings);
if (referBinding != null) {
measureName=OlapExpressionUtil.getMeasure(referBinding.getExpression());
bindingName=referBinding.getBindingName();
aggrOns=referBinding.getAggregatOns();
}
}
if (aggrOns != null && aggrOns.size() > 0) {
aggrOnLevels=new DimLevel[aggrOns.size()];
for (int j=0; j < aggrOnLevels.length; j++) {
aggrOnLevels[j]=OlapExpressionUtil.getTargetDimLevel(aggrOns.get(j).toString());
}
}
 else if (measureName != null) {
List measureAggrOns=CubeQueryDefinitionUtil.populateMeasureAggrOns(queryDefn);
aggrOnLevels=new DimLevel[measureAggrOns.size()];
for (int k=0; k < measureAggrOns.size(); k++) {
aggrOnLevels[k]=(DimLevel)measureAggrOns.get(k);
}
}
}
 else {
aggrOnLevels=new DimLevel[aggrOns.size()];
for (int j=0; j < aggrOnLevels.length; j++) {
aggrOnLevels[j]=OlapExpressionUtil.getTargetDimLevel(aggrOns.get(j).toString());
}
}
DimLevel[] axisLevels=new DimLevel[cubeSort.getAxisQualifierLevels().length];
for (int k=0; k < axisLevels.length; k++) {
axisLevels[k]=new DimLevel(cubeSort.getAxisQualifierLevels()[k]);
}
targetSort=new AggrSortDefinition(aggrOnLevels,bindingName,axisLevels,cubeSort.getAxisQualifierValues(),new DimLevel(cubeSort.getTargetLevel()),cubeSort.getSortDirection());
}
switch (type) {
case ICubeQueryDefinition.COLUMN_EDGE:
cubeQueryExcutorHelper.addColumnSort(targetSort);
break;
case ICubeQueryDefinition.ROW_EDGE:
cubeQueryExcutorHelper.addRowSort(targetSort);
break;
case ICubeQueryDefinition.PAGE_EDGE:
cubeQueryExcutorHelper.addPageSort(targetSort);
}
}
}