return rel.accept(
new RelShuttleImpl() {
@Override
public RelNode visit(TableAccessRelBase scan) {
RelOptTable relOptTable = scan.getTable();
final Table table = relOptTable.unwrap(Table.class);
if (table.equals(starTable.tables.get(0))) {
Mappings.TargetMapping mapping =
Mappings.createShiftMapping(
starRelOptTable.getRowType().getFieldCount(),
0, 0, relOptTable.getRowType().getFieldCount());
return CalcRel.createProject(
new TableAccessRel(scan.getCluster(), starRelOptTable),
Mappings.asList(mapping.inverse()));
}
return scan;
}
@Override
public RelNode visit(JoinRel join) {
for (;;) {
RelNode rel = super.visit(join);
if (rel == join || !(rel instanceof JoinRel)) {
return rel;
}
join = (JoinRel) rel;
final RelNode left = join.getLeft();
final RelNode right = join.getRight();
try {
if (left instanceof TableAccessRelBase
&& right instanceof TableAccessRelBase) {
match(left, null, right, null, join.getCluster());
}
if (isProjectedTable(left)
&& right instanceof TableAccessRelBase) {
final ProjectRel leftProject = (ProjectRel) left;
match(leftProject.getChild(), leftProject.getMapping(), right,
null, join.getCluster());
}
if (left instanceof TableAccessRelBase
&& isProjectedTable(right)) {
final ProjectRel rightProject = (ProjectRel) right;
match(left, null, rightProject.getChild(),
rightProject.getMapping(), join.getCluster());
}
if (isProjectedTable(left)
&& isProjectedTable(right)) {
final ProjectRel leftProject = (ProjectRel) left;
final ProjectRel rightProject = (ProjectRel) right;
match(leftProject.getChild(), leftProject.getMapping(),
rightProject.getChild(), rightProject.getMapping(),
join.getCluster());
}
} catch (Util.FoundOne e) {
return (RelNode) e.getNode();
}
}
}
private boolean isProjectedTable(RelNode rel) {
return rel instanceof ProjectRel
&& ((ProjectRel) rel).isMapping()
&& ((ProjectRel) rel).getChild() instanceof TableAccessRelBase;
}
/** Throws a {@link org.eigenbase.util.Util.FoundOne} containing a
* {@link org.eigenbase.rel.TableAccessRel} on success.
* (Yes, an exception for normal operation.) */
private void match(RelNode left, Mappings.TargetMapping leftMapping,
RelNode right, Mappings.TargetMapping rightMapping,
RelOptCluster cluster) {
if (leftMapping == null) {
leftMapping =
Mappings.createIdentity(left.getRowType().getFieldCount());
}
if (rightMapping == null) {
rightMapping =
Mappings.createIdentity(right.getRowType().getFieldCount());
}
final RelOptTable leftRelOptTable = left.getTable();
final Table leftTable = leftRelOptTable.unwrap(Table.class);
final RelOptTable rightRelOptTable = right.getTable();
final Table rightTable = rightRelOptTable.unwrap(Table.class);
if (leftTable instanceof StarTable
&& ((StarTable) leftTable).tables.contains(rightTable)) {
System.out.println("left: " + leftMapping);
System.out.println("right: " + rightMapping);
Mappings.TargetMapping mapping =