targetExpression = project.getFields()
.get(column.getPosition());
}
}
}
OrderByExpression indexColumn = getIndexColumn(indexOrdering, idx);
if (indexColumn == null && idx < nequals) {
throw new CorruptedPlanException("No index column expression for union comparison");
}
if (indexColumn != null) {
boolean matchingColumn = orderingExpressionMatches(indexColumn, targetExpression);
if (!matchingColumn && idx < nequals) {
// if we we're trying the union column, but that failed, try just treating it as equals
idx++;
indexColumn = getIndexColumn(indexOrdering, idx);
if (indexColumn != null) {
matchingColumn = orderingExpressionMatches(indexColumn, targetExpression);
if (matchingColumn) {
index.setIncludeUnionAsEquality(true);
}
}
}
if (matchingColumn) {
if (idx < nequals) {
index.setIncludeUnionAsEquality(false);
}
if (indexColumn.isAscending() != targetColumn.isAscending()) {
// To avoid mixed mode as much as possible,
// defer changing the index order until
// certain it will be effective.
reverse.set(idx, true);
if (idx == nequals)
// Likewise reverse the initial equals segment.
reverse.set(0, nequals, true);
}
if (idx >= index.getNKeyColumns())
index.setUsesAllColumns(true);
idx++;
continue;
}
}
if (equalityColumns != null) {
// Another possibility is that target ordering is
// in fact unchanged due to equality condition.
// TODO: Should this have been noticed earlier on
// so that it can be taken out of the sort?
if (equalityColumns.contains(targetExpression))
continue;
}
break try_sorted;
}
if ((idx > 0) && (idx < indexOrdering.size()) && reverse.get(idx-1))
// Reverse after ORDER BY if reversed last one.
reverse.set(idx, indexOrdering.size(), true);
for (int i = 0; i < reverse.size(); i++) {
if (reverse.get(i)) {
OrderByExpression indexColumn = indexOrdering.get(i);
indexColumn.setAscending(!indexColumn.isAscending());
}
}
result = IndexScan.OrderEffectiveness.SORTED;
}
if (queryGoal.getGrouping() != null) {