RelNode rel2 = 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());
final RelOptCluster cluster = scan.getCluster();
final RelNode scan2 =
starRelOptTable.toRel(RelOptUtil.getContext(cluster));
return RelOptUtil.createProject(scan2,
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 ProjectFilterTable left =
ProjectFilterTable.of(join.getLeft());
if (left != null) {
final ProjectFilterTable right =
ProjectFilterTable.of(join.getRight());
if (right != null) {
try {
match(left, right, join.getCluster());
} catch (Util.FoundOne e) {
return (RelNode) e.getNode();
}
}
}
}
}
/** 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(ProjectFilterTable left, ProjectFilterTable right,
RelOptCluster cluster) {
final Mappings.TargetMapping leftMapping = left.mapping();
final Mappings.TargetMapping rightMapping = right.mapping();
final RelOptTable leftRelOptTable = left.getTable();
final Table leftTable = leftRelOptTable.unwrap(Table.class);
final int leftCount = leftRelOptTable.getRowType().getFieldCount();
final RelOptTable rightRelOptTable = right.getTable();
final Table rightTable = rightRelOptTable.unwrap(Table.class);
if (leftTable instanceof StarTable
&& ((StarTable) leftTable).tables.contains(rightTable)) {
final int offset =
((StarTable) leftTable).columnOffset(rightTable);
Mappings.TargetMapping mapping =