public class TransformOrderByDistinctAppplication extends TransformCopy {
@Override
public Op transform(OpDistinct opDistinct, Op subOp) {
if (subOp instanceof OpProject) {
OpProject project = (OpProject) subOp;
// At the project stage everything is a simple variable
// Inner operation must be an ORDER BY
if (project.getSubOp() instanceof OpOrder) {
List<Var> projectVars = project.getVars();
OpOrder order = (OpOrder) project.getSubOp();
// Everything we wish to order by must only use variables that
// appear in the project list
boolean ok = true;
for (SortCondition condition : order.getConditions()) {
if (!isValidSortCondition(condition, projectVars)) {
ok = false;
break;
}
}
// Everything checks out so we can make the change
if (ok) {
OpProject newProject = new OpProject(order.getSubOp(), project.getVars());
OpDistinct newDistinct = new OpDistinct(newProject);
return new OpOrder(newDistinct, order.getConditions());
}
}
}