HashJoin extends JoinExecutorBase<BindingSet> { static CloseableIteration<BindingSet, QueryEvaluationException> join(Collection<BindingSet> leftBlock, Collection<BindingSet> rightBlock, Set<String> joinVariables, Collection<String> freeVariablesRight) { List<BindingSet> res = new LinkedList<>(); for (BindingSet left : leftBlock) { for (BindingSet right : rightBlock) { boolean match = false; for (String joinVariable : joinVariables) { Value leftValue = left.getValue(joinVariable); Value rightValue = right.getValue(joinVariable); if (leftValue != null && leftValue.equals(rightValue)) { match = true; break; } } for (String freeVariable : freeVariablesRight) { if (joinVariables.contains(freeVariable)) { continue; } Value leftValue = left.getValue(freeVariable); Value rightValue = right.getValue(freeVariable); if (leftValue != null && leftValue.equals(rightValue)) { match = true; } } if (match) { MapBindingSet mergedBindings = new MapBindingSet(); for (Binding b : left) { mergedBindings.addBinding(b); } for (Binding b : right) { mergedBindings.addBinding(b); } res.add(mergedBindings); } } } return new CollectionIteration<>(res); } HashJoin(FederationEvalStrategy strategy,
			CloseableIteration<BindingSet, QueryEvaluationException> leftIter,
			TupleExpr rightArg, Set<String> joinVars, BindingSet bindings, QueryInfo queryInfo);   }
@Test public void testSimple() throws Exception { List<BindingSet> leftBlock = new ArrayList<>(); leftBlock.add(bindingSet(binding("x", irid("p1")))); leftBlock.add(bindingSet(binding("x", irid("p2")))); leftBlock.add(bindingSet(binding("x", irid("p3")))); List<BindingSet> rightBlock = new ArrayList<>(); rightBlock.add(bindingSet(binding("x", irid("p2")))); rightBlock.add(bindingSet(binding("x", irid("p1")))); rightBlock.add(bindingSet(binding("x", irid("p4")))); CloseableIteration<BindingSet, QueryEvaluationException> joinResultIter = HashJoin.join(leftBlock, rightBlock, Sets.newHashSet("x"), Collections.emptyList()); List<BindingSet> joinResult = Iterations.asList(joinResultIter); Assertions.assertEquals(Lists.newArrayList( bindingSet(binding("x", irid("p1"))), bindingSet(binding("x", irid("p2")))), joinResult); }
@Test public void testMultipleBindings() throws Exception { List<BindingSet> leftBlock = new ArrayList<>(); leftBlock.add(bindingSet(binding("x", irid("p1")), binding("y", l("P1")))); leftBlock.add(bindingSet(binding("x", irid("p2")), binding("y", l("P2")))); List<BindingSet> rightBlock = new ArrayList<>(); rightBlock.add(bindingSet(binding("x", irid("p2")), binding("z", l("something")))); CloseableIteration<BindingSet, QueryEvaluationException> joinResultIter = HashJoin.join(leftBlock, rightBlock, Sets.newHashSet("x"), Collections.emptyList()); List<BindingSet> joinResult = Iterations.asList(joinResultIter); Assertions.assertEquals(1, joinResult.size()); Assertions.assertEquals( bindingSet(binding("x", irid("p2")), binding("y", l("P2")), binding("z", l("something"))), joinResult.get(0)); }
@Test public void testLeftJoin_NoResultForBinding() throws Exception { List<BindingSet> leftBlock = new ArrayList<>(); leftBlock.add(bindingSet(binding("x", irid("p1")), binding("y", l("P1")))); leftBlock.add(bindingSet(binding("y", l("P2")))); List<BindingSet> rightBlock = new ArrayList<>(); rightBlock.add(bindingSet(binding("x", irid("p1")), binding("z", l("something")))); CloseableIteration<BindingSet, QueryEvaluationException> joinResultIter = HashJoin.join(leftBlock, rightBlock, Sets.newHashSet("x"), Collections.emptyList()); List<BindingSet> joinResult = Iterations.asList(joinResultIter); Assertions.assertEquals(1, joinResult.size()); Assertions.assertEquals( bindingSet(binding("x", irid("p1")), binding("y", l("P1")), binding("z", l("something"))), joinResult.get(0)); }