@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")); }
@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; }
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; } }
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; }  }
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(); }
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; }