org.eclipse.rdf4j.query.algebra.Join Java Examples

The following examples show how to use org.eclipse.rdf4j.query.algebra.Join. 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: 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 #2
Source File: PeriodicQueryUtilTest.java    From rya with Apache License 2.0 6 votes vote down vote up
@Test
public void testPeriodicNodePlacement() throws MalformedQueryException {
     String query = "prefix function: <http://org.apache.rya/function#> " //n
            + "prefix time: <http://www.w3.org/2006/time#> " //n
            + "prefix fn: <http://www.w3.org/2006/fn#> " //n
            + "select ?obs ?time ?lat where {" //n
            + "Filter(function:periodic(?time, 12.0, 6.0,time:hours)) " //n
            + "Filter(fn:test(?lat, 25)) " //n
            + "?obs <uri:hasTime> ?time. " //n
            + "?obs <uri:hasLattitude> ?lat }"; //n
     
     SPARQLParser parser = new SPARQLParser();
     ParsedQuery pq = parser.parseQuery(query, null);
     TupleExpr te = pq.getTupleExpr();
     te.visit(new PeriodicQueryNodeVisitor());
     
     PeriodicNodeCollector collector = new PeriodicNodeCollector();
     te.visit(collector);
     
     PeriodicQueryNode node2 = new PeriodicQueryNode(12*60*60*1000L, 6*3600*1000L, TimeUnit.MILLISECONDS, "time", new Join());

     Assert.assertEquals(true, periodicNodesEqualIgnoreArg(node2, collector.getPeriodicQueryNode()));
     
}
 
Example #3
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 #4
Source File: FlattenedOptional.java    From rya with Apache License 2.0 6 votes vote down vote up
/**
 * This method is used to retrieve a set view of all descendants of the
 * rightArg of the LeftJoin (the optional part)
 *
 * @param tupleExpr
 *            - tupleExpr whose args are being retrieved
 * @param joinArgs
 *            - set view of all non-join args that are descendants of
 *            tupleExpr
 * @return joinArgs
 */
private Set<TupleExpr> getJoinArgs(TupleExpr tupleExpr, Set<TupleExpr> joinArgs) {
    if (tupleExpr instanceof Join) {
        if (!(((Join) tupleExpr).getLeftArg() instanceof FixedStatementPattern)
                && !(((Join) tupleExpr).getRightArg() instanceof DoNotExpandSP)) {
            Join join = (Join) tupleExpr;
            getJoinArgs(join.getLeftArg(), joinArgs);
            getJoinArgs(join.getRightArg(), joinArgs);
        }
    } else if (tupleExpr instanceof LeftJoin) { // TODO probably not
                                                // necessary if not
                                                // including leftarg
        LeftJoin lj = (LeftJoin) tupleExpr;
        joinArgs.add(new FlattenedOptional(lj));
        getJoinArgs(lj.getLeftArg(), joinArgs);
    } else if (tupleExpr instanceof Filter) {
        getJoinArgs(((Filter) tupleExpr).getArg(), joinArgs);
    } else {
        joinArgs.add(tupleExpr);
    }

    return joinArgs;
}
 
Example #5
Source File: QueryJoinOptimizerTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void bindSubselectJoinOrder() throws Exception {
	String query = "SELECT * WHERE {\n" + "    BIND (bnode() as ?ct01) \n" + "    { SELECT ?s WHERE {\n"
			+ "            ?s ?p ?o .\n" + "      }\n" + "      LIMIT 10\n" + "    }\n" + "}";

	SPARQLParser parser = new SPARQLParser();
	ParsedQuery q = parser.parseQuery(query, null);
	QueryJoinOptimizer opt = new QueryJoinOptimizer();
	QueryRoot optRoot = new QueryRoot(q.getTupleExpr());
	opt.optimize(optRoot, null, null);

	JoinFinder joinFinder = new JoinFinder();
	optRoot.visit(joinFinder);
	Join join = joinFinder.getJoin();

	assertThat(join.getLeftArg()).as("BIND clause should be left-most argument of join")
			.isInstanceOf(Extension.class);
}
 
Example #6
Source File: GeoEnabledFilterFunctionOptimizer.java    From rya with Apache License 2.0 6 votes vote down vote up
private void buildQuery(final TupleExpr tupleExpr, final StatementPattern matchStatement) {
    //If our IndexerExpr (to be) is the rhs-child of LeftJoin, we can safely make that a Join:
    //  the IndexerExpr will (currently) not return results that can deliver unbound variables.
    //This optimization should probably be generalized into a LeftJoin -> Join optimizer under certain conditions. Until that
    //  has been done, this code path at least takes care of queries generated by OpenSahara SparqTool that filter on OPTIONAL
    //  projections. E.g. summary~'full text search' (summary is optional). See #379
    if (matchStatement.getParentNode() instanceof LeftJoin) {
        final LeftJoin leftJoin = (LeftJoin)matchStatement.getParentNode();
        if (leftJoin.getRightArg() == matchStatement && leftJoin.getCondition() == null) {
            matchStatement.getParentNode().replaceWith(new Join(leftJoin.getLeftArg(), leftJoin.getRightArg()));
        }
    }
    final FilterFunction fVisitor = new FilterFunction(matchStatement.getObjectVar().getName());
    tupleExpr.visit(fVisitor);
    final List<IndexingExpr> results = Lists.newArrayList();
    for(int i = 0; i < fVisitor.func.size(); i++){
        results.add(new IndexingExpr(fVisitor.func.get(i), matchStatement, fVisitor.args.get(i)));
    }
    removeMatchedPattern(tupleExpr, matchStatement, new IndexerExprReplacer(results));
}
 
Example #7
Source File: TupleReArranger.java    From rya with Apache License 2.0 6 votes vote down vote up
private static List<TupleExpr> getJoinArgs(TupleExpr tupleExpr, List<TupleExpr> joinArgs) {
    if (tupleExpr instanceof Join) {
        if (!(((Join) tupleExpr).getLeftArg() instanceof FixedStatementPattern)
                && !(((Join) tupleExpr).getRightArg() instanceof DoNotExpandSP)) {
            Join join = (Join) tupleExpr;
            getJoinArgs(join.getLeftArg(), joinArgs);
            getJoinArgs(join.getRightArg(), joinArgs);
        } // assumes all filter occur above first join of segment --
          // this should be the state
          // after PrecompJoinOptimizer is called
    } else if (tupleExpr instanceof Filter) {
        filterList.add((Filter) tupleExpr);
        getJoinArgs(((Filter) tupleExpr).getArg(), joinArgs);
    } else {
        joinArgs.add(tupleExpr);
    }

    return joinArgs;
}
 
Example #8
Source File: SparqlToPipelineTransformVisitorTest.java    From rya with Apache License 2.0 6 votes vote down vote up
@Test
public void testMultiProjection() throws Exception {
    StatementPattern isUndergrad = new StatementPattern(new Var("x"), constant(RDF.TYPE), constant(UNDERGRAD));
    StatementPattern isCourse = new StatementPattern(new Var("course"), constant(RDF.TYPE), constant(COURSE));
    StatementPattern hasEdge = new StatementPattern(new Var("x"), new Var("p"), new Var("course"));
    ProjectionElemList courseHasRelation = new ProjectionElemList(
            new ProjectionElem("p", "relation"),
            new ProjectionElem("course"));
    ProjectionElemList studentHasRelation = new ProjectionElemList(
            new ProjectionElem("p", "relation"),
            new ProjectionElem("x", "student"));
    QueryRoot queryTree = new QueryRoot(new MultiProjection(
            new Join(new Join(isCourse, hasEdge), isUndergrad),
            Arrays.asList(courseHasRelation, studentHasRelation)));
    SparqlToPipelineTransformVisitor visitor = new SparqlToPipelineTransformVisitor(collection);
    queryTree.visit(visitor);
    Assert.assertTrue(queryTree.getArg() instanceof AggregationPipelineQueryNode);
    AggregationPipelineQueryNode pipelineNode = (AggregationPipelineQueryNode) queryTree.getArg();
    Assert.assertEquals(Sets.newHashSet("relation"), pipelineNode.getAssuredBindingNames());
    Assert.assertEquals(Sets.newHashSet("relation", "course", "student"), pipelineNode.getBindingNames());
}
 
Example #9
Source File: TupleExprs.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Verifies if the supplied {@link TupleExpr} contains a {@link Projection} with the subquery flag set to true
 * (default). If the supplied TupleExpr is a {@link Join} or contains a {@link Join}, projections inside that Join's
 * arguments will not be taken into account.
 *
 * @param t a tuple expression.
 * @return <code>true</code> if the TupleExpr contains a subquery projection (outside of a Join), <code>false</code>
 *         otherwise.
 */
public static boolean containsSubquery(TupleExpr t) {
	Deque<TupleExpr> queue = new ArrayDeque<>();
	queue.add(t);
	while (!queue.isEmpty()) {
		TupleExpr n = queue.removeFirst();
		if (n instanceof Projection && ((Projection) n).isSubquery()) {
			return true;
		} else if (n instanceof Join) {
			// projections already inside a Join need not be
			// taken into account
			return false;
		} else {
			queue.addAll(getChildren(n));
		}
	}
	return false;
}
 
Example #10
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 #11
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 #12
Source File: SPARQLParserTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testSES1927UnequalLiteralValueConstants1() throws Exception {

	StringBuilder qb = new StringBuilder();
	qb.append("ASK {?a <foo:bar> \"test\". ?a <foo:foo> \"test\"@en .} ");

	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();

	assertFalse(leftArg.getObjectVar().equals(rightArg.getObjectVar()));
	assertNotEquals(leftArg.getObjectVar().getName(), rightArg.getObjectVar().getName());
}
 
Example #13
Source File: JoinSegment.java    From rya with Apache License 2.0 6 votes vote down vote up
/**
 *
 * @param tupleExpr
 *            - the query object that will be traversed by this method
 * @param joinArgs
 *            - all nodes connected by Joins and Filters
 * @return - List containing all nodes connected by Joins, LeftJoins, and
 *         Filters. This List contains the
 * @param ValueExpr
 *            in place of the Filter
 */
private List<QueryModelNode> getJoinArgs(TupleExpr tupleExpr, List<QueryModelNode> joinArgs) {

    if (tupleExpr instanceof Join) {
        if (!(((Join) tupleExpr).getLeftArg() instanceof FixedStatementPattern)
                && !(((Join) tupleExpr).getRightArg() instanceof DoNotExpandSP)) {
            Join join = (Join) tupleExpr;
            getJoinArgs(join.getRightArg(), joinArgs);
            getJoinArgs(join.getLeftArg(), joinArgs);
        }
    } else if (tupleExpr instanceof Filter) {
        Filter filter = (Filter) tupleExpr;
        joinArgs.add(filter.getCondition());
        conditionMap.put(filter.getCondition(), filter);
        getJoinArgs(filter.getArg(), joinArgs);
    } else {
        joinArgs.add(tupleExpr);
    }
    return joinArgs;
}
 
Example #14
Source File: SparqlToPipelineTransformVisitorTest.java    From rya with Apache License 2.0 6 votes vote down vote up
@Test
public void testNestedJoins() 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(
            isProfessor,
            new Join(
                    new Join(isUndergrad, takesCourse),
                    teachesCourse)));
    SparqlToPipelineTransformVisitor visitor = new SparqlToPipelineTransformVisitor(collection);
    queryTree.visit(visitor);
    Assert.assertTrue(queryTree.getArg() instanceof AggregationPipelineQueryNode);
    AggregationPipelineQueryNode pipelineNode = (AggregationPipelineQueryNode) queryTree.getArg();
    Assert.assertEquals(Sets.newHashSet("x", "y", "c"), pipelineNode.getAssuredBindingNames());
}
 
Example #15
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 #16
Source File: HalyardTupleExprEvaluation.java    From Halyard with Apache License 2.0 6 votes vote down vote up
/**
 * Evaluate {@link BinaryTupleOperator} query model nodes
 * @param parent
 * @param expr
 * @param bindings
 */
private void evaluateBinaryTupleOperator(BindingSetPipe parent, BinaryTupleOperator expr, BindingSet bindings) {
    if (expr instanceof Join) {
        evaluateJoin(parent, (Join) expr, bindings);
    } else if (expr instanceof LeftJoin) {
        evaluateLeftJoin(parent, (LeftJoin) expr, bindings);
    } else if (expr instanceof Union) {
        evaluateUnion(parent, (Union) expr, bindings);
    } else if (expr instanceof Intersection) {
        evaluateIntersection(parent, (Intersection) expr, bindings);
    } else if (expr instanceof Difference) {
        evaluateDifference(parent, (Difference) expr, bindings);
    } else if (expr == null) {
        parent.handleException(new IllegalArgumentException("expr must not be null"));
    } else {
        parent.handleException(new QueryEvaluationException("Unsupported binary tuple operator type: " + expr.getClass()));
    }
}
 
Example #17
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 #18
Source File: FederatedEvaluationStrategyImpl.java    From semagrow with Apache License 2.0 6 votes vote down vote up
@Override
public Flux<BindingSet> evaluateReactorInternal(Join expr, BindingSet bindings)
        throws QueryEvaluationException
{
    if (expr instanceof BindJoin) {
        return evaluateReactorInternal((BindJoin) expr, bindings);
    }
    else if (expr instanceof HashJoin) {
        return evaluateReactorInternal((HashJoin) expr, bindings);
    }
    else if (expr instanceof MergeJoin) {
        return evaluateReactorInternal((MergeJoin) expr, bindings);
    }
    else if (expr == null) {
        throw new IllegalArgumentException("expr must not be null");
    }
    else {
        throw new QueryEvaluationException("Unsupported tuple expr type: " + expr.getClass());
    }
}
 
Example #19
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 #20
Source File: QueryJoinOptimizer.java    From rya with Apache License 2.0 5 votes vote down vote up
protected <L extends List<TupleExpr>> L getJoinArgs(TupleExpr tupleExpr, L joinArgs) {
    if (tupleExpr instanceof Join) {
        Join join = (Join) tupleExpr;
        getJoinArgs(join.getLeftArg(), joinArgs);
        getJoinArgs(join.getRightArg(), joinArgs);
    } else {
        joinArgs.add(tupleExpr);
    }

    return joinArgs;
}
 
Example #21
Source File: BasicGraphPatternExtractor.java    From CostFed with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
	public void meet(Join join) {
		
//		boolean valid = true;
//		
//		// visit join arguments and check that all are valid BGPS
//		for (TupleExpr expr : new TupleExpr[] { join.getLeftArg(), join.getRightArg() }) {
//			expr.visit(this);
//			if (lastBGPNode == null) {
//				// child is not a BGP -> join is not a BGP
//				valid = false;
//			} else {
//				if (!valid) {
//					// last child is a BGP but another child was not
//					this.bgpList.add(lastBGPNode);
//					lastBGPNode = null;
//				}
//			}
//		}
//		if (valid)
//			lastBGPNode = join;
		
		
		///
		join.getLeftArg().visit(this);
		TupleExpr leftLastBGPNode = lastBGPNode;
		lastBGPNode = null;
		join.getRightArg().visit(this);
		if (lastBGPNode != null && leftLastBGPNode != null) {
			lastBGPNode = join;
		} else if (lastBGPNode != null) {
			bgpList.add(lastBGPNode);
			lastBGPNode = null;
		} else if (leftLastBGPNode != null) { // lastBGPNode == null here
			bgpList.add(leftLastBGPNode);
		}
	}
 
Example #22
Source File: GenericInfoOptimizer.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void meet(Join node) {

	/*
	 * Optimization task:
	 *
	 * Collect all join arguments recursively and create the NJoin structure for easier join order optimization
	 */

	NJoin newJoin = OptimizerUtil.flattenJoin(node, queryInfo);
	newJoin.visitChildren(this);

	node.replaceWith(newJoin);
}
 
Example #23
Source File: SparqlToPipelineTransformVisitorTest.java    From rya with Apache License 2.0 5 votes vote down vote up
@Test
public void testJoin() throws Exception {
    QueryRoot query = new QueryRoot(new Join(
            new StatementPattern(new Var("x"), constant(RDF.TYPE), constant(UNDERGRAD)),
            new StatementPattern(new Var("x"), constant(TAKES), new Var("course"))));
    SparqlToPipelineTransformVisitor visitor = new SparqlToPipelineTransformVisitor(collection);
    query.visit(visitor);
    Assert.assertTrue(query.getArg() instanceof AggregationPipelineQueryNode);
    AggregationPipelineQueryNode pipelineNode = (AggregationPipelineQueryNode) query.getArg();
    Assert.assertEquals(Sets.newHashSet("x", "course"), pipelineNode.getAssuredBindingNames());
}
 
Example #24
Source File: TupleReArranger.java    From rya with Apache License 2.0 5 votes vote down vote up
private static List<Map<Join, List<TupleExpr>>> getReOrderings(List<Join> joins) {
    Map<Join, List<TupleExpr>> reOrder = Maps.newHashMap();
    List<Map<Join, List<TupleExpr>>> reOrderings = Lists.newArrayList();
    getReOrderings(joins, reOrder, reOrderings);
    return reOrderings;

}
 
Example #25
Source File: OptimizerUtil.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Collect join arguments by descending the query tree (recursively).
 *
 * @param node
 * @param joinArgs
 */
protected static void collectJoinArgs(TupleExpr node, List<TupleExpr> joinArgs) {

	if (node instanceof Join) {
		collectJoinArgs(((Join) node).getLeftArg(), joinArgs);
		collectJoinArgs(((Join) node).getRightArg(), joinArgs);
	} else {
		joinArgs.add(node);
	}
}
 
Example #26
Source File: JoinSegmentTest.java    From rya with Apache License 2.0 5 votes vote down vote up
@Test
public void testNoMatchTooLarge() throws Exception {

	String query1 = ""//
			+ "SELECT ?e ?c ?l" //
			+ "{" //
			+ "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . "//
			+ "  ?e a ?c . "//
			+ "  ?e <uri:talksTo> ?l  . "//
			+ "}";//

	String query2 = ""//
			+ "SELECT ?e ?c ?l" //
			+ "{" //
			+ " ?e a ?c . "//
			+ " ?e <uri:worksAt> ?l . "//
			+ "}";//

	SPARQLParser parser = new SPARQLParser();
	ParsedQuery pq1 = parser.parseQuery(query1, null);
	ParsedQuery pq2 = parser.parseQuery(query2, null);
	TupleExpr te1 = pq1.getTupleExpr();
	TupleExpr te2 = pq2.getTupleExpr();
	TopOfQueryFilterRelocator.moveFiltersToTop(te1);
	TopOfQueryFilterRelocator.moveFiltersToTop(te1);
	Join join1 = (Join) ((Projection) te1).getArg();
	Join join2 = (Join) ((Projection) te2).getArg();

	QuerySegment<ExternalTupleSet> seg1 = qFactory.getQuerySegment(join1);
       QuerySegment<ExternalTupleSet> seg2 = qFactory.getQuerySegment(join2);

	Assert.assertEquals(false, seg2.containsQuerySegment(seg1));

}
 
Example #27
Source File: GenericInfoOptimizer.java    From CostFed with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void meet(Join node) {
	
	/*
	 * Optimization task:
	 * 
	 * Collect all join arguments recursively and create the
	 * NJoin structure for easier join order optimization
	 */
			
	NJoin newJoin = OptimizerUtil.flattenJoin(node, queryInfo);
	int withoutStatementChildrenCount = 0;
	List<StatementPattern> accumPatterns = null;
	for (TupleExpr child : newJoin.getArgs())
	{
		child.visit(this);
		if (curStmts.isEmpty()) {
			++withoutStatementChildrenCount;
		} else if (accumPatterns == null) {
			accumPatterns = curStmts;
			curStmts = new ArrayList<StatementPattern>();
		} else {
			accumPatterns.addAll(curStmts);
			curStmts = new ArrayList<StatementPattern>();
		}
	}
	
	node.replaceWith(newJoin);
	curStmts = accumPatterns;
	if (withoutStatementChildrenCount != 0) {
		beginNewBGPGroup();
	}
}
 
Example #28
Source File: PCJNodeConsolidatorTest.java    From rya with Apache License 2.0 5 votes vote down vote up
@Test
public void testInvalidOrder() throws Exception {

	String query1 = ""//
			+ "SELECT ?a ?b ?c ?d" //
			+ "{" //
			+ "  ?a <uri:p5> <uri:const3>" //
			+ "  OPTIONAL{<uri:const2> <uri:p4> ?d } . "//
			+ "  ?a <uri:p4> ?b . "//
			+ "  OPTIONAL{?a <uri:p3> ?c} . " //
			+ "  OPTIONAL{<uri:const1> <uri:p2> ?b} . "//
			+ "  ?c <uri:p1> ?d "
			+ "}";//

	String query2 = ""//
			+ "SELECT ?a ?b ?c ?d" //
			+ "{" //
			+ "  ?a <uri:p4> ?b . "//
			+ "  ?c <uri:p1> ?d "
			+ "  OPTIONAL{<uri:const2> <uri:p4> ?d } . "//
			+ "}";//

	SPARQLParser parser = new SPARQLParser();
	ParsedQuery pq1 = parser.parseQuery(query1, null);
	ParsedQuery pq2 = parser.parseQuery(query2, null);
	TupleExpr te1 = pq1.getTupleExpr();
	TupleExpr te2 = pq2.getTupleExpr();
	Join join1 = (Join) ((Projection) te1).getArg();
	LeftJoin join2 = (LeftJoin) ((Projection) te2).getArg();

	QuerySegment<ExternalTupleSet> seg1 = qFactory.getQuerySegment(join1);
    QuerySegment<ExternalTupleSet> seg2 = qFactory.getQuerySegment(join2);

	QueryNodeConsolidator consolidator = new QueryNodeConsolidator(seg1.getOrderedNodes(), seg2.getOrderedNodes());

	Assert.assertTrue(!consolidator.consolidateNodes());
}
 
Example #29
Source File: PeriodicQueryUtilTest.java    From rya with Apache License 2.0 5 votes vote down vote up
@Test
public void periodicNodePresentTest() throws Exception {
    
    List<ValueExpr> values = Arrays.asList(new Var("time"), new ValueConstant(VF.createLiteral(12.0)), new ValueConstant(VF.createLiteral(6.0)), new ValueConstant(VF.createIRI(PeriodicQueryUtil.temporalNameSpace + "hours")));
    FunctionCall func = new FunctionCall(PeriodicQueryUtil.PeriodicQueryURI, values);
    Optional<PeriodicQueryNode> node1 = PeriodicQueryUtil.getPeriodicQueryNode(func, new Join());
    Assert.assertEquals(true, node1.isPresent());
    
    PeriodicQueryNode node2 = new PeriodicQueryNode(12*60*60*1000L, 6*3600*1000L, TimeUnit.MILLISECONDS, "time", new Join());
    
    Assert.assertEquals(true, periodicNodesEqualIgnoreArg(node1.get(), node2));
}
 
Example #30
Source File: HalyardQueryJoinOptimizerTest.java    From Halyard with Apache License 2.0 5 votes vote down vote up
@Test
public void testQueryJoinOptimizer() {
    final TupleExpr expr = new SPARQLParser().parseQuery("select * where {?a ?b ?c, \"1\".}", "http://baseuri/").getTupleExpr();
    new HalyardQueryJoinOptimizer(new HalyardEvaluationStatistics(null, null)).optimize(expr, null, null);
    expr.visit(new AbstractQueryModelVisitor<RuntimeException>(){
        @Override
        public void meet(Join node) throws RuntimeException {
            assertTrue(expr.toString(), ((StatementPattern)node.getLeftArg()).getObjectVar().hasValue());
            assertEquals(expr.toString(), "c", ((StatementPattern)node.getRightArg()).getObjectVar().getName());
        }
    });
}