@Test
public void shouldOptimizePlanForQueryUsingViewContainingJoin() {

    node = optimize("SELECT v2.c11 AS c1 FROM v2 WHERE v2.c11 = 'x' AND v2.c12 = 'y'");
    multipleSelectors = true;
    // Create the expected plan ...
    PlanNode project = new PlanNode(Type.PROJECT, selector("t1"));
    project.setProperty(Property.PROJECT_COLUMNS, columns(column("t1", "c11", "c1")));
    PlanNode join = new PlanNode(Type.JOIN, project, selector("t2"), selector("t1"));
    join.setProperty(Property.JOIN_ALGORITHM, JoinAlgorithm.NESTED_LOOP);
    join.setProperty(Property.JOIN_TYPE, JoinType.INNER);
    join.setProperty(Property.JOIN_CONDITION, new EquiJoinCondition(selector("t1"), "c11", selector("t2"), "c21"));
    PlanNode leftAccess = new PlanNode(Type.ACCESS, join, selector("t1"));
    PlanNode leftProject = new PlanNode(Type.PROJECT, leftAccess, selector("t1"));
    leftProject.setProperty(Property.PROJECT_COLUMNS, columns(column("t1", "c11", "c1")));
    PlanNode leftSelect1 = new PlanNode(Type.SELECT, leftProject, selector("t1"));
    leftSelect1.setProperty(Property.SELECT_CRITERIA, new Comparison(new PropertyValue(selector("t1"), "c11"),
                                                                     Operator.EQUAL_TO, new Literal('x')));
    PlanNode leftSelect2 = new PlanNode(Type.SELECT, leftSelect1, selector("t1"));
    leftSelect2.setProperty(Property.SELECT_CRITERIA, new Comparison(new PropertyValue(selector("t1"), "c12"),
                                                                     Operator.EQUAL_TO, new Literal('y')));
    PlanNode leftSource = new PlanNode(Type.SOURCE, leftSelect2, selector("t1"));
    leftSource.setProperty(Property.SOURCE_NAME, selector("t1"));
    leftSource.setProperty(Property.SOURCE_COLUMNS, context.getSchemata().getTable(selector("t1")).getColumns());
    PlanNode rightAccess = new PlanNode(Type.ACCESS, join, selector("t2"));
    PlanNode rightProject = new PlanNode(Type.PROJECT, rightAccess, selector("t2"));
    rightProject.setProperty(Property.PROJECT_COLUMNS, columns(nonSelectedColumn("t2", "c21")));
    PlanNode rightSelect1 = new PlanNode(Type.SELECT, rightProject, selector("t2"));
    rightSelect1.setProperty(Property.SELECT_CRITERIA, new Comparison(new PropertyValue(selector("t2"), "c21"),
                                                                      Operator.EQUAL_TO, new Literal('x')));
    PlanNode rightSource = new PlanNode(Type.SOURCE, rightSelect1, selector("t2"));
    rightSource.setProperty(Property.SOURCE_NAME, selector("t2"));
    rightSource.setProperty(Property.SOURCE_COLUMNS, context.getSchemata().getTable(selector("t2")).getColumns());
    // Compare the expected and actual plan ...
    assertPlanMatches(project);
}