AddJoinConditionColumnsToSources implements OptimizerRule { @Override public PlanNode execute( QueryContext context, PlanNode plan, LinkedList<OptimizerRule> ruleStack ) { final boolean includeSourceName = context.getHints().qualifyExpandedColumnNames; for (PlanNode joinNode : plan.findAllAtOrBelow(Type.JOIN)) { JoinCondition condition = joinNode.getProperty(Property.JOIN_CONDITION, JoinCondition.class); if (condition instanceof EquiJoinCondition) { EquiJoinCondition equiJoinCondition = (EquiJoinCondition)condition; SelectorName selector1 = equiJoinCondition.selector1Name(); Column joinColumn1 = columnFor(equiJoinCondition.selector1Name(), equiJoinCondition.getProperty1Name(), includeSourceName); Column joinColumn2 = columnFor(equiJoinCondition.selector2Name(), equiJoinCondition.getProperty2Name(), includeSourceName); PlanNode left = joinNode.getFirstChild(); PlanNode right = joinNode.getLastChild(); if (left.getSelectors().contains(selector1)) { addEquiJoinColumn(context, left, joinColumn1); addEquiJoinColumn(context, right, joinColumn2); } else { addEquiJoinColumn(context, left, joinColumn2); addEquiJoinColumn(context, right, joinColumn1); } } } return plan; }  @Override PlanNode execute( QueryContext context,
                             PlanNode plan,
                             LinkedList<OptimizerRule> ruleStack ); @Override String toString(); static final AddJoinConditionColumnsToSources INSTANCE; }
@Test public void shouldNotAddJoinConditionColumnIfAlreadyUsed() { PlanNode joinNode = new PlanNode(Type.JOIN); joinNode.setProperty(Property.JOIN_TYPE, JoinType.LEFT_OUTER); joinNode.setProperty(Property.JOIN_CONDITION, new EquiJoinCondition(selector("left"), "c2", selector("right"), "d2")); PlanNode lhs = sourceNode(context, joinNode, "left", "c1", "c2", "c3"); PlanNode rhs = sourceNode(context, joinNode, "right", "d1", "d2", "d3"); PlanNode result = rule.execute(context, joinNode, new LinkedList<OptimizerRule>()); assertThat(result, is(sameInstance(joinNode))); assertThat(joinNode.getProperty(Property.JOIN_TYPE, JoinType.class), is(JoinType.LEFT_OUTER)); assertThat(joinNode.getFirstChild(), is(sameInstance(lhs))); assertThat(joinNode.getLastChild(), is(sameInstance(rhs))); assertThat(joinNode.getChildCount(), is(2)); PlanNode left = joinNode.getFirstChild(); PlanNode right = joinNode.getLastChild(); assertProperty(left, Property.PROJECT_COLUMNS, columns(context, selector("left"), "c1", "c2", "c3")); assertProperty(left, Property.PROJECT_COLUMN_TYPES, columnTypes(context, selector("left"), "c1", "c2", "c3")); assertProperty(right, Property.PROJECT_COLUMNS, columns(context, selector("right"), "d1", "d2", "d3")); assertProperty(right, Property.PROJECT_COLUMN_TYPES, columnTypes(context, selector("right"), "d1", "d2", "d3")); }
@Test public void shouldNotAddJoinConditionWithChildNodesSwapped() { PlanNode joinNode = new PlanNode(Type.JOIN); joinNode.setProperty(Property.JOIN_TYPE, JoinType.LEFT_OUTER); joinNode.setProperty(Property.JOIN_CONDITION, new EquiJoinCondition(selector("right"), "d2", selector("left"), "c2")); PlanNode lhs = sourceNode(context, joinNode, "left", "c1", "c2", "c3"); PlanNode rhs = sourceNode(context, joinNode, "right", "d1", "d2", "d3"); PlanNode result = rule.execute(context, joinNode, new LinkedList<OptimizerRule>()); assertThat(result, is(sameInstance(joinNode))); assertThat(joinNode.getProperty(Property.JOIN_TYPE, JoinType.class), is(JoinType.LEFT_OUTER)); assertThat(joinNode.getFirstChild(), is(sameInstance(lhs))); assertThat(joinNode.getLastChild(), is(sameInstance(rhs))); assertThat(joinNode.getChildCount(), is(2)); PlanNode left = joinNode.getFirstChild(); PlanNode right = joinNode.getLastChild(); assertProperty(left, Property.PROJECT_COLUMNS, columns(context, selector("left"), "c1", "c2", "c3")); assertProperty(left, Property.PROJECT_COLUMN_TYPES, columnTypes(context, selector("left"), "c1", "c2", "c3")); assertProperty(right, Property.PROJECT_COLUMNS, columns(context, selector("right"), "d1", "d2", "d3")); assertProperty(right, Property.PROJECT_COLUMN_TYPES, columnTypes(context, selector("right"), "d1", "d2", "d3")); }
@Test public void shouldAddJoinConditionColumnOnLeftSideIfNotAlreadyUsed() { PlanNode joinNode = new PlanNode(Type.JOIN); joinNode.setProperty(Property.JOIN_TYPE, JoinType.LEFT_OUTER); joinNode.setProperty(Property.JOIN_CONDITION, new EquiJoinCondition(selector("left"), "c4", selector("right"), "d2")); PlanNode lhs = sourceNode(context, joinNode, "left", "c1", "c2", "c3"); PlanNode rhs = sourceNode(context, joinNode, "right", "d1", "d2", "d3"); PlanNode result = rule.execute(context, joinNode, new LinkedList<OptimizerRule>()); assertThat(result, is(sameInstance(joinNode))); assertThat(joinNode.getProperty(Property.JOIN_TYPE, JoinType.class), is(JoinType.LEFT_OUTER)); assertThat(joinNode.getFirstChild(), is(sameInstance(lhs))); assertThat(joinNode.getLastChild(), is(sameInstance(rhs))); assertThat(joinNode.getChildCount(), is(2)); PlanNode left = joinNode.getFirstChild(); PlanNode right = joinNode.getLastChild(); assertProperty(left, Property.PROJECT_COLUMNS, columns(context, selector("left"), "c1", "c2", "c3", "c4")); assertProperty(left, Property.PROJECT_COLUMN_TYPES, columnTypes(context, selector("left"), "c1", "c2", "c3", "c4")); assertProperty(right, Property.PROJECT_COLUMNS, columns(context, selector("right"), "d1", "d2", "d3")); assertProperty(right, Property.PROJECT_COLUMN_TYPES, columnTypes(context, selector("right"), "d1", "d2", "d3")); }
@Test public void shouldAddJoinConditionColumnOnRightSideIfNotAlreadyUsed() { PlanNode joinNode = new PlanNode(Type.JOIN); joinNode.setProperty(Property.JOIN_TYPE, JoinType.LEFT_OUTER); joinNode.setProperty(Property.JOIN_CONDITION, new EquiJoinCondition(selector("left"), "c2", selector("right"), "d4")); PlanNode lhs = sourceNode(context, joinNode, "left", "c1", "c2", "c3"); PlanNode rhs = sourceNode(context, joinNode, "right", "d1", "d2", "d3"); PlanNode result = rule.execute(context, joinNode, new LinkedList<OptimizerRule>()); assertThat(result, is(sameInstance(joinNode))); assertThat(joinNode.getProperty(Property.JOIN_TYPE, JoinType.class), is(JoinType.LEFT_OUTER)); assertThat(joinNode.getFirstChild(), is(sameInstance(lhs))); assertThat(joinNode.getLastChild(), is(sameInstance(rhs))); assertThat(joinNode.getChildCount(), is(2)); PlanNode left = joinNode.getFirstChild(); PlanNode right = joinNode.getLastChild(); assertProperty(left, Property.PROJECT_COLUMNS, columns(context, selector("left"), "c1", "c2", "c3")); assertProperty(left, Property.PROJECT_COLUMN_TYPES, columnTypes(context, selector("left"), "c1", "c2", "c3")); assertProperty(right, Property.PROJECT_COLUMNS, columns(context, selector("right"), "d1", "d2", "d3", "d4")); assertProperty(right, Property.PROJECT_COLUMN_TYPES, columnTypes(context, selector("right"), "d1", "d2", "d3", "d4")); }
@Test public void shouldAddJoinConditionColumnOnBothSidesIfNotAlreadyUsed() { PlanNode joinNode = new PlanNode(Type.JOIN); joinNode.setProperty(Property.JOIN_TYPE, JoinType.LEFT_OUTER); joinNode.setProperty(Property.JOIN_CONDITION, new EquiJoinCondition(selector("left"), "c4", selector("right"), "d4")); PlanNode lhs = sourceNode(context, joinNode, "left", "c1", "c2", "c3"); PlanNode rhs = sourceNode(context, joinNode, "right", "d1", "d2", "d3"); PlanNode result = rule.execute(context, joinNode, new LinkedList<OptimizerRule>()); assertThat(result, is(sameInstance(joinNode))); assertThat(joinNode.getProperty(Property.JOIN_TYPE, JoinType.class), is(JoinType.LEFT_OUTER)); assertThat(joinNode.getFirstChild(), is(sameInstance(lhs))); assertThat(joinNode.getLastChild(), is(sameInstance(rhs))); assertThat(joinNode.getChildCount(), is(2)); PlanNode left = joinNode.getFirstChild(); PlanNode right = joinNode.getLastChild(); assertProperty(left, Property.PROJECT_COLUMNS, columns(context, selector("left"), "c1", "c2", "c3", "c4")); assertProperty(left, Property.PROJECT_COLUMN_TYPES, columnTypes(context, selector("left"), "c1", "c2", "c3", "c4")); assertProperty(right, Property.PROJECT_COLUMNS, columns(context, selector("right"), "d1", "d2", "d3", "d4")); assertProperty(right, Property.PROJECT_COLUMN_TYPES, columnTypes(context, selector("right"), "d1", "d2", "d3", "d4")); }
@Test public void shouldAddJoinConditionColumnOnBothSidesIfChildrenSwappedAndIfNotAlreadyUsed() { PlanNode joinNode = new PlanNode(Type.JOIN); joinNode.setProperty(Property.JOIN_TYPE, JoinType.LEFT_OUTER); joinNode.setProperty(Property.JOIN_CONDITION, new EquiJoinCondition(selector("right"), "d4", selector("left"), "c4")); PlanNode lhs = sourceNode(context, joinNode, "left", "c1", "c2", "c3"); PlanNode rhs = sourceNode(context, joinNode, "right", "d1", "d2", "d3"); PlanNode result = rule.execute(context, joinNode, new LinkedList<OptimizerRule>()); assertThat(result, is(sameInstance(joinNode))); assertThat(joinNode.getProperty(Property.JOIN_TYPE, JoinType.class), is(JoinType.LEFT_OUTER)); assertThat(joinNode.getFirstChild(), is(sameInstance(lhs))); assertThat(joinNode.getLastChild(), is(sameInstance(rhs))); assertThat(joinNode.getChildCount(), is(2)); PlanNode left = joinNode.getFirstChild(); PlanNode right = joinNode.getLastChild(); assertProperty(left, Property.PROJECT_COLUMNS, columns(context, selector("left"), "c1", "c2", "c3", "c4")); assertProperty(left, Property.PROJECT_COLUMN_TYPES, columnTypes(context, selector("left"), "c1", "c2", "c3", "c4")); assertProperty(right, Property.PROJECT_COLUMNS, columns(context, selector("right"), "d1", "d2", "d3", "d4")); assertProperty(right, Property.PROJECT_COLUMN_TYPES, columnTypes(context, selector("right"), "d1", "d2", "d3", "d4")); }