Java Code Examples for org.eclipse.rdf4j.query.algebra.Join#getRightArg()

The following examples show how to use org.eclipse.rdf4j.query.algebra.Join#getRightArg() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SPARQLParserTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testSES1922PathSequenceWithValueConstant() throws Exception {

	StringBuilder qb = new StringBuilder();
	qb.append("ASK {?A (<foo:bar>)/<foo:foo> <foo:objValue>} ");

	ParsedQuery q = parser.parseQuery(qb.toString(), null);
	TupleExpr te = q.getTupleExpr();

	assertNotNull(te);

	assertTrue(te instanceof Slice);
	Slice s = (Slice) te;
	assertTrue(s.getArg() instanceof Join);
	Join j = (Join) s.getArg();

	assertTrue(j.getLeftArg() instanceof StatementPattern);
	assertTrue(j.getRightArg() instanceof StatementPattern);
	StatementPattern leftArg = (StatementPattern) j.getLeftArg();
	StatementPattern rightArg = (StatementPattern) j.getRightArg();

	assertTrue(leftArg.getObjectVar().equals(rightArg.getSubjectVar()));
	assertEquals(leftArg.getObjectVar().getName(), rightArg.getSubjectVar().getName());
}
 
Example 2
Source File: SparqlToPipelineTransformVisitorTest.java    From rya with Apache License 2.0 6 votes vote down vote up
@Test
public void testComplexJoin() throws Exception {
    StatementPattern isUndergrad = new StatementPattern(new Var("x"), constant(RDF.TYPE), constant(UNDERGRAD));
    StatementPattern isProfessor = new StatementPattern(new Var("y"), constant(RDF.TYPE), constant(PROFESSOR));
    StatementPattern takesCourse = new StatementPattern(new Var("x"), constant(TAKES), new Var("c"));
    StatementPattern teachesCourse = new StatementPattern(new Var("y"), constant(TEACHES), new Var("c"));
    QueryRoot queryTree = new QueryRoot(new Join(
            new Join(isUndergrad, takesCourse),
            new Join(isProfessor, teachesCourse)));
    SparqlToPipelineTransformVisitor visitor = new SparqlToPipelineTransformVisitor(collection);
    queryTree.visit(visitor);
    Assert.assertTrue(queryTree.getArg() instanceof Join);
    Join topJoin = (Join) queryTree.getArg();
    Assert.assertTrue(topJoin.getLeftArg() instanceof AggregationPipelineQueryNode);
    Assert.assertTrue(topJoin.getRightArg() instanceof AggregationPipelineQueryNode);
    AggregationPipelineQueryNode leftPipeline = (AggregationPipelineQueryNode) topJoin.getLeftArg();
    AggregationPipelineQueryNode rightPipeline = (AggregationPipelineQueryNode) topJoin.getRightArg();
    Assert.assertEquals(Sets.newHashSet("x", "c"), leftPipeline.getAssuredBindingNames());
    Assert.assertEquals(Sets.newHashSet("y", "c"), rightPipeline.getAssuredBindingNames());
}
 
Example 3
Source File: RdfCloudTripleStoreSelectivityEvaluationStatistics.java    From rya with Apache License 2.0 6 votes vote down vote up
@Override
    public void meet(final Join node) {
      node.getLeftArg().visit(this);
      final double leftArgCost = cardinality;
      // System.out.println("Left cardinality is " + cardinality);
      node.getRightArg().visit(this);

      if (node.getLeftArg() instanceof FixedStatementPattern && node.getRightArg() instanceof DoNotExpandSP) {
        return;
      }

      try {
        final double selectivity = selectEvalStatsDAO.getJoinSelect(config, node.getLeftArg(), node.getRightArg());
//        System.out.println("CardCalc: left cost of " + node.getLeftArg() + " is " + leftArgCost + " right cost of "
//        + node.getRightArg() + " is " + cardinality);
//         System.out.println("Right cardinality is " + cardinality);
        cardinality += leftArgCost + leftArgCost * cardinality * selectivity;
//        System.out.println("CardCalc: Cardinality is " + cardinality);
//        System.out.println("CardCalc: Selectivity is " + selectivity);
        // System.out.println("Join cardinality is " + cardinality);

      } catch (final Exception e) {
        e.printStackTrace();
      }

    }
 
Example 4
Source File: SeparateFilterJoinsVisitor.java    From rya with Apache License 2.0 6 votes vote down vote up
@Override
public void meet(final Filter node) throws Exception {
    super.meet(node);

    final ValueExpr condition = node.getCondition();
    final TupleExpr arg = node.getArg();
    if (!(arg instanceof Join)) {
        return;
    }

    final Join join = (Join) arg;
    final TupleExpr leftArg = join.getLeftArg();
    final TupleExpr rightArg = join.getRightArg();

    if (leftArg instanceof StatementPattern && rightArg instanceof StatementPattern) {
        final Filter left = new Filter(leftArg, condition);
        final Filter right = new Filter(rightArg, condition);
        node.replaceWith(new Join(left, right));
    }

}
 
Example 5
Source File: PushJoinDownVisitor.java    From rya with Apache License 2.0 6 votes vote down vote up
@Override
public void meet(final Join node) throws Exception {
    super.meet(node);

    final TupleExpr leftArg = node.getLeftArg();
    final TupleExpr rightArg = node.getRightArg();

    /**
     * if join(join(1, 2), join(3,4))
     * should be:
     * join(join(join(1,2), 3), 4)
     */
    if (leftArg instanceof Join && rightArg instanceof Join) {
        final Join leftJoin = (Join) leftArg;
        final Join rightJoin = (Join) rightArg;
        final TupleExpr right_LeftArg = rightJoin.getLeftArg();
        final TupleExpr right_rightArg = rightJoin.getRightArg();
        final Join inner = new Join(leftJoin, right_LeftArg);
        final Join outer = new Join(inner, right_rightArg);
        node.replaceWith(outer);
    }

}
 
Example 6
Source File: EntityOptimizer.java    From rya with Apache License 2.0 6 votes vote down vote up
@Override
public void meet(Join node) {
    try {
        if (node.getLeftArg() instanceof FixedStatementPattern && node.getRightArg() instanceof DoNotExpandSP) {
            return;
        }
        List<TupleExpr> joinArgs = getJoinArgs(node, new ArrayList<TupleExpr>());
        HashMultimap<String, StatementPattern> varMap = getVarBins(joinArgs);
        while (!varMap.keySet().isEmpty()) {
            String s = getHighestPriorityKey(varMap);
            constructTuple(varMap, joinArgs, s);
        }
        List<TupleExpr> filterChain = getFilterChain(joinArgs);

        for (TupleExpr te : joinArgs) {
            if (!(te instanceof StatementPattern) || !(te instanceof EntityTupleSet)) {
                te.visit(this);
            }
        }
        // Replace old join hierarchy
        node.replaceWith(getNewJoin(joinArgs, filterChain));

    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 7
Source File: GeneralizedExternalProcessor.java    From rya with Apache License 2.0 6 votes vote down vote up
@Override
public void meet(Join node) {

          if (compSet.contains(node.getRightArg())) {
              this.toBeReplaced = node.getRightArg();
              node.replaceChildNode(node.getRightArg(), replacement);
              return;
          } else if (compSet.contains(node.getLeftArg())) {
              this.toBeReplaced = node.getLeftArg();
              node.replaceChildNode(node.getLeftArg(), replacement);
              return;
          } else {
              super.meet(node);
          }

      }
 
Example 8
Source File: GeneralizedExternalProcessor.java    From rya with Apache License 2.0 6 votes vote down vote up
@Override
public void meet(Join queryNode) {

          // if query tree contains external tuples and they are not
          // positioned above statement pattern node
          // reposition
          if (this.bsas.size() > 0 && !(queryNode.getRightArg() instanceof BindingSetAssignment)) {
              QNodeExchanger qnev = new QNodeExchanger(queryNode.getRightArg(), bsas);
              queryNode.visit(qnev);
              queryNode.replaceChildNode(queryNode.getRightArg(), qnev.getReplaced());
              super.meet(queryNode);
          } else {
              super.meet(queryNode);
          }

      }
 
Example 9
Source File: BasicGroup.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private TupleExpr asJoin(Collection<TupleExpr> theList) {
	Join aJoin = new Join();

	if (theList.isEmpty()) {
		throw new RuntimeException("Can't have an empty or missing join.");
	} else if (theList.size() == 1) {
		return theList.iterator().next();
	}

	for (TupleExpr aExpr : theList) {
		if (aJoin.getLeftArg() == null) {
			aJoin.setLeftArg(aExpr);
		} else if (aJoin.getRightArg() == null) {
			aJoin.setRightArg(aExpr);
		} else {
			Join aNewJoin = new Join();

			aNewJoin.setLeftArg(aJoin);
			aNewJoin.setRightArg(aExpr);

			aJoin = aNewJoin;
		}
	}

	return aJoin;
}
 
Example 10
Source File: IndexPlanValidator.java    From rya with Apache License 2.0 6 votes vote down vote up
private boolean isJoinValid(Join join) {

        Set<String> leftBindingNames = join.getLeftArg().getBindingNames();
        Set<String> rightBindingNames = join.getRightArg().getBindingNames();

        
        //System.out.println("Left binding names are " + leftBindingNames + " and right binding names are " + rightBindingNames);
        
        if (Sets.intersection(leftBindingNames, rightBindingNames).size() == 0) {
            return !omitCrossProd;
        } else {
            if (join.getRightArg() instanceof ExternalTupleSet) {
                return ((ExternalTupleSet) join.getRightArg()).supportsBindingSet(leftBindingNames);
            } else {
                return true;
            }
        }

    }
 
Example 11
Source File: LimitedSizeEvaluationStrategy.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public CloseableIteration<BindingSet, QueryEvaluationException> evaluate(Join join, BindingSet bindings)
		throws QueryEvaluationException {
	// efficient computation of a SERVICE join using vectored evaluation
	// TODO maybe we can create a ServiceJoin node already in the parser?
	if (join.getRightArg() instanceof Service) {
		CloseableIteration<BindingSet, QueryEvaluationException> leftIter = evaluate(join.getLeftArg(), bindings);
		return new ServiceJoinIterator(leftIter, (Service) join.getRightArg(), bindings, this);
	}

	if (TupleExprs.containsSubquery(join.getRightArg())) {
		return new LimitedSizeHashJoinIteration(this, join, bindings, used, maxSize);
	} else {
		return new JoinIterator(this, join, bindings);
	}
}
 
Example 12
Source File: GeneralizedExternalProcessor.java    From rya with Apache License 2.0 6 votes vote down vote up
@Override
public void meet(Join queryNode) {

          // if query tree contains external tuples and they are not
          // positioned above statement pattern node
          // reposition
          if (this.extTuples.size() > 0 && !(queryNode.getRightArg() instanceof ExternalTupleSet)
                  && !(queryNode.getRightArg() instanceof BindingSetAssignment)) {

              if (queryNode.getLeftArg() instanceof ExternalTupleSet) {
                  QueryModelNode temp = queryNode.getLeftArg();
                  queryNode.setLeftArg(queryNode.getRightArg());
                  queryNode.setRightArg((TupleExpr)temp);
              } else {

                  QNodeExchanger qnev = new QNodeExchanger(queryNode.getRightArg(), this.extTuples);
                  queryNode.visit(qnev);
                  queryNode.replaceChildNode(queryNode.getRightArg(), qnev.getReplaced());
                  super.meet(queryNode);
              }
          } else {
              super.meet(queryNode);
          }

      }
 
Example 13
Source File: StrictEvaluationStrategy.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public CloseableIteration<BindingSet, QueryEvaluationException> evaluate(Join join, BindingSet bindings)
		throws QueryEvaluationException {
	// efficient computation of a SERVICE join using vectored evaluation
	// TODO maybe we can create a ServiceJoin node already in the parser?
	if (join.getRightArg() instanceof Service) {
		CloseableIteration<BindingSet, QueryEvaluationException> leftIter = evaluate(join.getLeftArg(), bindings);
		return new ServiceJoinIterator(leftIter, (Service) join.getRightArg(), bindings, this);
	}

	if (isOutOfScopeForLeftArgBindings(join.getRightArg())) {
		return new HashJoinIteration(this, join, bindings);
	} else {
		return new JoinIterator(this, join, bindings);
	}
}
 
Example 14
Source File: SPARQLParserTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testWildCardPathComplexSubjectHandling() {

	String query = "PREFIX : <http://example.org/>\n ASK { ?a (:comment/^(:subClassOf|(:type/:label))/:type)* ?b } ";

	ParsedQuery parsedQuery = parser.parseQuery(query, null);
	TupleExpr tupleExpr = parsedQuery.getTupleExpr();

	Slice slice = (Slice) tupleExpr;

	ArbitraryLengthPath path = (ArbitraryLengthPath) slice.getArg();
	Var pathStart = path.getSubjectVar();
	Var pathEnd = path.getObjectVar();

	assertThat(pathStart.getName()).isEqualTo("a");
	assertThat(pathEnd.getName()).isEqualTo("b");

	Join pathSequence = (Join) path.getPathExpression();
	Join innerJoin = (Join) pathSequence.getLeftArg();
	Var commentObjectVar = ((StatementPattern) innerJoin.getLeftArg()).getObjectVar();

	Union union = (Union) innerJoin.getRightArg();
	Var subClassOfSubjectVar = ((StatementPattern) union.getLeftArg()).getSubjectVar();
	assertThat(subClassOfSubjectVar).isNotEqualTo(commentObjectVar);

	Var subClassOfObjectVar = ((StatementPattern) union.getLeftArg()).getObjectVar();

	assertThat(subClassOfObjectVar).isEqualTo(commentObjectVar);
}
 
Example 15
Source File: TestSparqlStarParser.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testUseInStatementPatternWithVars() throws Exception {
	String simpleSparqlQuery = "SELECT * WHERE { <<?s ?p ?o>> <urn:pred> ?val}";

	ParsedQuery q = parser.parseQuery(simpleSparqlQuery, null);

	assertNotNull(q);
	assertTrue("expect projection", q.getTupleExpr() instanceof Projection);
	Projection proj = (Projection) q.getTupleExpr();
	List<ProjectionElem> list = proj.getProjectionElemList().getElements();
	final ArrayList<String> listNames = new ArrayList<>();
	list.forEach(el -> {
		listNames.add(el.getTargetName());
	});
	assertEquals("expect all bindings", 4, list.size());
	assertTrue("expect s", listNames.contains("s"));
	assertTrue("expect p", listNames.contains("p"));
	assertTrue("expect o", listNames.contains("o"));
	assertTrue("expect val", listNames.contains("val"));

	assertTrue("expect Join", proj.getArg() instanceof Join);
	Join join = (Join) proj.getArg();

	assertTrue("expect right arg of Join be StatementPattern", join.getRightArg() instanceof StatementPattern);
	StatementPattern pattern = (StatementPattern) join.getRightArg();
	String anonVar = pattern.getSubjectVar().getName();
	assertEquals("statement pattern predVar value", "urn:pred", pattern.getPredicateVar().getValue().toString());
	assertEquals("statement pattern obj var name", "val", pattern.getObjectVar().getName());

	assertTrue("expect left arg of Join be TripleRef", join.getLeftArg() instanceof TripleRef);
	TripleRef triple = (TripleRef) join.getLeftArg();

	assertEquals("ext var should match", anonVar, triple.getExprVar().getName());

	assertEquals("subj var name should match", "s", triple.getSubjectVar().getName());
	assertEquals("pred var name should match", "p", triple.getPredicateVar().getName());
	assertEquals("obj var name should match", "o", triple.getObjectVar().getName());
}
 
Example 16
Source File: EvaluationStatistics.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void meet(Join node) {
	double cost = 1;
	for (TupleExpr arg : new TupleExpr[] { node.getLeftArg(), // NOPMD
			node.getRightArg() }) {
		arg.visit(this);
		cost *= this.cardinality;
	}
	cardinality = cost;
}
 
Example 17
Source File: QueryModelPruner.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void meet(Join join) {
	super.meet(join);

	TupleExpr leftArg = join.getLeftArg();
	TupleExpr rightArg = join.getRightArg();

	if (leftArg instanceof EmptySet || rightArg instanceof EmptySet) {
		join.replaceWith(new EmptySet());
	} else if (leftArg instanceof SingletonSet) {
		join.replaceWith(rightArg);
	} else if (rightArg instanceof SingletonSet) {
		join.replaceWith(leftArg);
	}
}
 
Example 18
Source File: GeneralizedExternalProcessor.java    From rya with Apache License 2.0 5 votes vote down vote up
@Override
		public void meet(Join node) {

            Set<QueryModelNode> eSet = getQNodes(node);

            if (eSet.containsAll(sSet) && !(node.getRightArg() instanceof BindingSetAssignment)) {

//                System.out.println("Eset is " + eSet + " and sSet is " + sSet);

                if (eSet.equals(sSet)) {
                    node.replaceWith(set);
                    indexPlaced = true;
                    return;
                } else {
                    if (node.getLeftArg() instanceof StatementPattern && sSet.size() == 1) {
                        if(sSet.contains(node.getLeftArg())) {
                            node.setLeftArg(set);
                            indexPlaced = true;
                        } else if(sSet.contains(node.getRightArg())) {
                            node.setRightArg(set);
                            indexPlaced = true;
                        } else {
                            return;
                        }
                    }
                    else {
                        super.meet(node);
                    }
                }
            } else if (eSet.containsAll(sSet)) {

                super.meet(node);

            } else {
                return;
            }

        }
 
Example 19
Source File: HashJoinIteration.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public HashJoinIteration(EvaluationStrategy strategy, Join join, BindingSet bindings)
		throws QueryEvaluationException {
	this(strategy, join.getLeftArg(), join.getRightArg(), bindings, false);
	join.setAlgorithm(this);
}
 
Example 20
Source File: TestSparqlStarParser.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Test
public void testUseNestedInStatementPatternWithVars() throws Exception {
	String simpleSparqlQuery = "SELECT * WHERE { <<<<?s ?p ?o>> ?q ?r>> <urn:pred> ?val}";

	ParsedQuery q = parser.parseQuery(simpleSparqlQuery, null);

	assertNotNull(q);
	assertTrue("expect projection", q.getTupleExpr() instanceof Projection);
	Projection proj = (Projection) q.getTupleExpr();
	List<ProjectionElem> list = proj.getProjectionElemList().getElements();
	final ArrayList<String> listNames = new ArrayList<>();
	list.forEach(el -> {
		listNames.add(el.getTargetName());
	});
	assertEquals("expect all bindings", 6, list.size());
	assertTrue("expect s", listNames.contains("s"));
	assertTrue("expect p", listNames.contains("p"));
	assertTrue("expect o", listNames.contains("o"));
	assertTrue("expect q", listNames.contains("q"));
	assertTrue("expect r", listNames.contains("r"));
	assertTrue("expect val", listNames.contains("val"));

	assertTrue("expect Join", proj.getArg() instanceof Join);
	Join join = (Join) proj.getArg();

	assertTrue("expect right arg of Join be StatementPattern", join.getRightArg() instanceof StatementPattern);
	StatementPattern pattern = (StatementPattern) join.getRightArg();
	String anonVar = pattern.getSubjectVar().getName();
	assertEquals("statement pattern predVar value", "urn:pred", pattern.getPredicateVar().getValue().toString());
	assertEquals("statement pattern obj var name", "val", pattern.getObjectVar().getName());

	assertTrue("expect left arg of first Join be Join", join.getLeftArg() instanceof Join);
	Join join2 = (Join) join.getLeftArg();

	assertTrue("expect left arg of second Join be TripleRef", join2.getLeftArg() instanceof TripleRef);
	TripleRef tripleLeft = (TripleRef) join2.getLeftArg();
	assertEquals("subj var name should match", "s", tripleLeft.getSubjectVar().getName());
	assertEquals("pred var name should match", "p", tripleLeft.getPredicateVar().getName());
	assertEquals("obj var name should match", "o", tripleLeft.getObjectVar().getName());
	String anonVarLeftTripleRef = tripleLeft.getExprVar().getName();

	assertTrue("expect right arg of second Join be TripleRef", join2.getRightArg() instanceof TripleRef);
	TripleRef triple = (TripleRef) join2.getRightArg();

	assertEquals("subj var name should match anon", anonVarLeftTripleRef, triple.getSubjectVar().getName());
	assertEquals("pred var name should match", "q", triple.getPredicateVar().getName());
	assertEquals("obj var name should match", "r", triple.getObjectVar().getName());

	assertEquals("ext var should match", anonVar, triple.getExprVar().getName());
}