Search in sources :

Example 46 with QueryModelNode

use of org.openrdf.query.algebra.QueryModelNode in project incubator-rya by apache.

the class QueryNodesToTupleExpr method getNewJoin.

// build newJoin node given remaining joinArgs and chain of filters
private static TupleExpr getNewJoin(List<QueryModelNode> args, List<Filter> filterChain) {
    TupleExpr newJoin;
    TupleExpr tempJoin;
    final List<TupleExpr> joinArgs = Lists.newArrayList();
    for (QueryModelNode q : args) {
        if (q instanceof TupleExpr) {
            joinArgs.add(0, (TupleExpr) q);
        } else {
            throw new IllegalArgumentException("Invalid query node!");
        }
    }
    if (joinArgs.size() > 1) {
        TupleExpr left = joinArgs.remove(0);
        TupleExpr right = joinArgs.remove(0);
        tempJoin = getJoin(left, right);
        for (int i = joinArgs.size() - 1; i >= 0; i--) {
            tempJoin = getJoin(tempJoin, joinArgs.get(i));
        }
        if (filterChain.size() == 0) {
            newJoin = tempJoin;
        } else if (filterChain.size() == 1) {
            newJoin = filterChain.get(0);
            ((Filter) newJoin).setArg(tempJoin);
        } else {
            newJoin = filterChain.get(0);
            filterChain.get(1).setArg(tempJoin);
        }
    } else if (joinArgs.size() == 1) {
        tempJoin = joinArgs.get(0);
        if (filterChain.size() == 0) {
            newJoin = tempJoin;
        } else if (filterChain.size() == 1) {
            newJoin = filterChain.get(0);
            ((Filter) newJoin).setArg(tempJoin);
        } else {
            newJoin = filterChain.get(0);
            filterChain.get(1).setArg(tempJoin);
        }
    } else {
        throw new IllegalStateException("JoinArgs size cannot be zero.");
    }
    return newJoin;
}
Also used : Filter(org.openrdf.query.algebra.Filter) QueryModelNode(org.openrdf.query.algebra.QueryModelNode) TupleExpr(org.openrdf.query.algebra.TupleExpr)

Example 47 with QueryModelNode

use of org.openrdf.query.algebra.QueryModelNode in project incubator-rya by apache.

the class OptionalJoinSegment method replaceWithExternalSet.

/**
 * This method matches the ordered nodes returned by
 * {@link OptionalJoinSegment #getOrderedNodes()} for nodeToReplace with a subset of
 * the ordered nodes for this OptionalJoinSegment. The order of the nodes for
 * nodeToReplace must match the order of the nodes as a subset of
 * orderedNodes
 *
 * @param segment
 *            - nodes to be replaced by ExternalSet node
 * @param set
 *            - ExternalSet node that will replace specified query nodes
 */
@Override
public boolean replaceWithExternalSet(QuerySegment<T> segment, T set) {
    Preconditions.checkNotNull(segment != null);
    Preconditions.checkNotNull(set);
    if (!containsQuerySegment(segment)) {
        return false;
    }
    List<QueryModelNode> nodeList = segment.getOrderedNodes();
    int begin = orderedNodes.indexOf(nodeList.get(0));
    // TODO this assumes no duplicate nodes
    if (begin < 0 || begin + nodeList.size() > orderedNodes.size() || !nodeList.equals(orderedNodes.subList(begin, begin + nodeList.size()))) {
        return false;
    }
    orderedNodes.removeAll(nodeList);
    orderedNodes.add(begin, set);
    unorderedNodes.removeAll(nodeList);
    unorderedNodes.add(set);
    for (QueryModelNode q : nodeList) {
        if (q instanceof ValueExpr) {
            conditionMap.remove(q);
        }
    }
    return true;
}
Also used : ValueExpr(org.openrdf.query.algebra.ValueExpr) QueryModelNode(org.openrdf.query.algebra.QueryModelNode)

Example 48 with QueryModelNode

use of org.openrdf.query.algebra.QueryModelNode in project incubator-rya by apache.

the class QueryNodeConsolidator method updateLeftJoinNodes.

// updates the var counts in specified left join index
private void updateLeftJoinNodes(PositionNode node, int finalPos) {
    if (node.getNode() instanceof ValueExpr) {
        return;
    }
    int diff = finalPos - node.getPosition();
    if (diff == 0) {
        return;
    }
    if (node.isOptional) {
        leftJoinPosSet.remove(node);
        FlattenedOptional optional = (FlattenedOptional) node.getNode();
        if (diff < 0) {
            for (int i = node.getPosition() - 1; i > finalPos - 1; i--) {
                QueryModelNode tempNode = queryNodes.get(i);
                if (tempNode instanceof ValueExpr) {
                    continue;
                }
                optional.addArg((TupleExpr) tempNode);
            }
        } else {
            for (int i = node.getPosition() + 1; i < finalPos + 1; i++) {
                QueryModelNode tempNode = queryNodes.get(i);
                if (tempNode instanceof ValueExpr) {
                    continue;
                }
                optional.removeArg((TupleExpr) tempNode);
            }
        }
        node.setNode(optional);
        // FlattenedOptional equals does not take into account var counts
        // The following three lines update the var count in the optional in
        // list
        int index = queryNodes.indexOf(optional);
        queryNodes.remove(optional);
        queryNodes.add(index, optional);
        leftJoinPosSet.add(node);
    } else {
        TupleExpr te = (TupleExpr) node.getNode();
        SortedSet<PositionNode> optionals;
        if (diff < 0) {
            optionals = leftJoinPosSet.subSet(new PositionNode(finalPos), true, node, false);
            for (PositionNode pNode : optionals) {
                FlattenedOptional optional = (FlattenedOptional) pNode.getNode();
                optional.removeArg(te);
            }
        } else {
            optionals = leftJoinPosSet.subSet(node, false, new PositionNode(finalPos), true);
            for (PositionNode pNode : optionals) {
                FlattenedOptional optional = (FlattenedOptional) pNode.getNode();
                optional.addArg(te);
            }
        }
    }
}
Also used : ValueExpr(org.openrdf.query.algebra.ValueExpr) QueryModelNode(org.openrdf.query.algebra.QueryModelNode) TupleExpr(org.openrdf.query.algebra.TupleExpr)

Example 49 with QueryModelNode

use of org.openrdf.query.algebra.QueryModelNode in project incubator-rya by apache.

the class SparqlFluoQueryBuilder method getConstructGraphVarOrder.

private static VariableOrder getConstructGraphVarOrder(final Reduced node) {
    // get child node
    final QueryModelNode child = node.getArg();
    Preconditions.checkArgument(child instanceof Projection || child instanceof MultiProjection);
    final UnaryTupleOperator unary = (UnaryTupleOperator) child;
    // get ProjectionElemList to build ConstructGraph
    final List<ProjectionElemList> projections = new ArrayList<>();
    if (unary instanceof Projection) {
        projections.add(((Projection) unary).getProjectionElemList());
    } else {
        projections.addAll(((MultiProjection) unary).getProjections());
    }
    return getConstructGraphVarOrder(projections);
}
Also used : ProjectionElemList(org.openrdf.query.algebra.ProjectionElemList) UnaryTupleOperator(org.openrdf.query.algebra.UnaryTupleOperator) ArrayList(java.util.ArrayList) QueryModelNode(org.openrdf.query.algebra.QueryModelNode) MultiProjection(org.openrdf.query.algebra.MultiProjection) Projection(org.openrdf.query.algebra.Projection) ConstructProjection(org.apache.rya.indexing.pcj.fluo.app.ConstructProjection) MultiProjection(org.openrdf.query.algebra.MultiProjection)

Example 50 with QueryModelNode

use of org.openrdf.query.algebra.QueryModelNode in project incubator-rya by apache.

the class GeneralizedExternalProcessor method getVarNames.

private static Set<String> getVarNames(Collection<QueryModelNode> nodes) {
    List<String> tempVars;
    Set<String> nodeVarNames = Sets.newHashSet();
    for (QueryModelNode s : nodes) {
        tempVars = VarCollector.process(s);
        for (String t : tempVars) {
            nodeVarNames.add(t);
        }
    }
    return nodeVarNames;
}
Also used : QueryModelNode(org.openrdf.query.algebra.QueryModelNode)

Aggregations

QueryModelNode (org.openrdf.query.algebra.QueryModelNode)98 TupleExpr (org.openrdf.query.algebra.TupleExpr)74 Test (org.junit.Test)68 ArrayList (java.util.ArrayList)63 ParsedQuery (org.openrdf.query.parser.ParsedQuery)63 SPARQLParser (org.openrdf.query.parser.sparql.SPARQLParser)62 ExternalTupleSet (org.apache.rya.indexing.external.tupleSet.ExternalTupleSet)56 SimpleExternalTupleSet (org.apache.rya.indexing.external.tupleSet.SimpleExternalTupleSet)48 StatementPattern (org.openrdf.query.algebra.StatementPattern)33 PCJOptimizer (org.apache.rya.indexing.pcj.matching.PCJOptimizer)27 HashSet (java.util.HashSet)26 Projection (org.openrdf.query.algebra.Projection)23 Filter (org.openrdf.query.algebra.Filter)15 LeftJoin (org.openrdf.query.algebra.LeftJoin)12 Join (org.openrdf.query.algebra.Join)11 ValueExpr (org.openrdf.query.algebra.ValueExpr)11 QueryNodeConsolidator (org.apache.rya.indexing.external.matching.QueryNodeConsolidator)8 Configuration (org.apache.hadoop.conf.Configuration)7 AccumuloIndexSetProvider (org.apache.rya.indexing.pcj.matching.provider.AccumuloIndexSetProvider)7 BatchWriter (org.apache.accumulo.core.client.BatchWriter)6