Search in sources :

Example 81 with QueryBindingSet

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

the class AccumuloPcjSerializer method convert.

@Override
public BindingSet convert(byte[] bindingSetBytes, VariableOrder varOrder) throws BindingSetConversionException {
    checkNotNull(bindingSetBytes);
    checkNotNull(varOrder);
    try {
        // Slice the row into bindings.
        List<byte[]> values = splitlByDelimByte(bindingSetBytes);
        String[] varOrderStrings = varOrder.toArray();
        checkArgument(values.size() == varOrderStrings.length);
        // Convert the Binding bytes into a BindingSet.
        final QueryBindingSet bindingSet = new QueryBindingSet();
        for (int i = 0; i < varOrderStrings.length; i++) {
            byte[] valueBytes = values.get(i);
            if (valueBytes.length > 0) {
                String name = varOrderStrings[i];
                Value value = deserializeValue(valueBytes);
                bindingSet.addBinding(name, value);
            }
        }
        return bindingSet;
    } catch (RyaTypeResolverException e) {
        throw new BindingSetConversionException("Could not convert the byte[] into a BindingSet.", e);
    }
}
Also used : Value(org.openrdf.model.Value) RyaTypeResolverException(org.apache.rya.api.resolver.RyaTypeResolverException) QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet)

Example 82 with QueryBindingSet

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

the class AccumuloIndexSet method getConstantConstraints.

/**
 * @return - all constraints which correspond to variables in
 *         {@link AccumuloIndexSet#getTupleExpr()} which are set equal to a
 *         constant, but are non-constant in Accumulo table
 */
private BindingSet getConstantConstraints() {
    final Map<String, String> tableMap = this.getTableVarMap();
    final Set<String> keys = tableMap.keySet();
    final QueryBindingSet constants = new QueryBindingSet();
    for (final String s : keys) {
        if (s.startsWith("-const-")) {
            constants.addBinding(new BindingImpl(s, getConstantValueMap().get(s)));
        }
    }
    return constants;
}
Also used : BindingImpl(org.openrdf.query.impl.BindingImpl) QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet)

Example 83 with QueryBindingSet

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

the class AccumuloIndexSet method evaluate.

/**
 * Core evaluation method used during query evaluation - given a collection
 * of binding set constraints, this method finds common binding labels
 * between the constraints and table, uses those to build a prefix scan of
 * the Accumulo table, and creates a solution binding set by iterating of
 * the scan results.
 * @param bindingset - collection of {@link BindingSet}s to be joined with PCJ
 * @return - CloseableIteration over joined results
 */
@Override
public CloseableIteration<BindingSet, QueryEvaluationException> evaluate(final Collection<BindingSet> bindingset) throws QueryEvaluationException {
    if (bindingset.isEmpty()) {
        return new IteratorWrapper<BindingSet, QueryEvaluationException>(new HashSet<BindingSet>().iterator());
    }
    final List<BindingSet> crossProductBs = new ArrayList<>();
    final Map<String, org.openrdf.model.Value> constantConstraints = new HashMap<>();
    final Set<Range> hashJoinRanges = new HashSet<>();
    final Range EMPTY_RANGE = new Range("", true, "~", false);
    Range crossProductRange = EMPTY_RANGE;
    String localityGroupOrder = varOrder.get(0);
    int maxPrefixLen = Integer.MIN_VALUE;
    int prefixLen = 0;
    int oldPrefixLen = 0;
    final Multimap<String, BindingSet> bindingSetHashMap = HashMultimap.create();
    HashJoinType joinType = HashJoinType.CONSTANT_JOIN_VAR;
    final Set<String> unAssuredVariables = Sets.difference(getTupleExpr().getBindingNames(), getTupleExpr().getAssuredBindingNames());
    boolean useColumnScan = false;
    boolean isCrossProd = false;
    boolean containsConstantConstraints = false;
    final BindingSet constants = getConstantConstraints();
    containsConstantConstraints = constants.size() > 0;
    try {
        for (final BindingSet bs : bindingset) {
            if (bindingset.size() == 1 && bs.size() == 0) {
                // in this case, only single, empty bindingset, pcj node is
                // first node in query plan - use full Range scan with
                // column
                // family set
                useColumnScan = true;
            }
            // get common vars for PCJ - only use variables associated
            // with assured Bindings
            final QueryBindingSet commonVars = new QueryBindingSet();
            for (final String b : getTupleExpr().getAssuredBindingNames()) {
                final Binding v = bs.getBinding(b);
                if (v != null) {
                    commonVars.addBinding(v);
                }
            }
            // no common vars implies cross product
            if (commonVars.size() == 0 && bs.size() != 0) {
                crossProductBs.add(bs);
                isCrossProd = true;
            }
            // get a varOrder from orders in PCJ table - use at least
            // one common variable
            final BindingSetVariableOrder varOrder = getVarOrder(commonVars.getBindingNames(), constants.getBindingNames());
            // update constant constraints not used in varOrder and
            // update Bindings used to form range by removing unused
            // variables
            commonVars.addAll(constants);
            if (commonVars.size() > varOrder.varOrderLen) {
                final Map<String, Value> valMap = getConstantValueMap();
                for (final String s : new HashSet<String>(varOrder.unusedVars)) {
                    if (valMap.containsKey(s) && !constantConstraints.containsKey(s)) {
                        constantConstraints.put(s, valMap.get(s));
                    }
                    commonVars.removeBinding(s);
                }
            }
            if (containsConstantConstraints && (useColumnScan || isCrossProd)) {
                // constant constraints
                if (crossProductRange == EMPTY_RANGE) {
                    crossProductRange = getRange(varOrder.varOrder, commonVars);
                    localityGroupOrder = prefixToOrder(varOrder.varOrder);
                }
            } else if (!useColumnScan && !isCrossProd) {
                // update ranges and add BindingSet to HashJoinMap if not a
                // cross product
                hashJoinRanges.add(getRange(varOrder.varOrder, commonVars));
                prefixLen = varOrder.varOrderLen;
                // update the HashJoinType to be VARIABLE_JOIN_VAR.
                if (oldPrefixLen == 0) {
                    oldPrefixLen = prefixLen;
                } else {
                    if (oldPrefixLen != prefixLen && joinType == HashJoinType.CONSTANT_JOIN_VAR) {
                        joinType = HashJoinType.VARIABLE_JOIN_VAR;
                    }
                    oldPrefixLen = prefixLen;
                }
                // update max prefix len
                if (prefixLen > maxPrefixLen) {
                    maxPrefixLen = prefixLen;
                }
                final String key = getHashJoinKey(varOrder.varOrder, commonVars);
                bindingSetHashMap.put(key, bs);
            }
            isCrossProd = false;
        }
        // BindingSets
        if ((useColumnScan || crossProductBs.size() > 0) && bindingSetHashMap.size() == 0) {
            final Scanner scanner = accCon.createScanner(tablename, auths);
            // cross product with no cross product constraints here
            scanner.setRange(crossProductRange);
            scanner.fetchColumnFamily(new Text(localityGroupOrder));
            return new PCJKeyToCrossProductBindingSetIterator(scanner, crossProductBs, constantConstraints, unAssuredVariables, getTableVarMap());
        } else if ((useColumnScan || crossProductBs.size() > 0) && bindingSetHashMap.size() > 0) {
            // in this case, both hash join BindingSets and cross product
            // BindingSets exist
            // create an iterator to evaluate cross product and an iterator
            // for hash join, then combine
            final List<CloseableIteration<BindingSet, QueryEvaluationException>> iteratorList = new ArrayList<>();
            // create cross product iterator
            final Scanner scanner1 = accCon.createScanner(tablename, auths);
            scanner1.setRange(crossProductRange);
            scanner1.fetchColumnFamily(new Text(localityGroupOrder));
            iteratorList.add(new PCJKeyToCrossProductBindingSetIterator(scanner1, crossProductBs, constantConstraints, unAssuredVariables, getTableVarMap()));
            // create hash join iterator
            final BatchScanner scanner2 = accCon.createBatchScanner(tablename, auths, 10);
            scanner2.setRanges(hashJoinRanges);
            final PCJKeyToJoinBindingSetIterator iterator = new PCJKeyToJoinBindingSetIterator(scanner2, getTableVarMap(), maxPrefixLen);
            iteratorList.add(new BindingSetHashJoinIterator(bindingSetHashMap, iterator, unAssuredVariables, joinType));
            // combine iterators
            return new IteratorCombiner(iteratorList);
        } else {
            // only hash join BindingSets exist
            final BatchScanner scanner = accCon.createBatchScanner(tablename, auths, 10);
            // only need to create hash join iterator
            scanner.setRanges(hashJoinRanges);
            final PCJKeyToJoinBindingSetIterator iterator = new PCJKeyToJoinBindingSetIterator(scanner, getTableVarMap(), maxPrefixLen);
            return new BindingSetHashJoinIterator(bindingSetHashMap, iterator, unAssuredVariables, joinType);
        }
    } catch (final Exception e) {
        throw new QueryEvaluationException(e);
    }
}
Also used : BatchScanner(org.apache.accumulo.core.client.BatchScanner) Scanner(org.apache.accumulo.core.client.Scanner) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) BatchScanner(org.apache.accumulo.core.client.BatchScanner) PCJKeyToJoinBindingSetIterator(org.apache.rya.accumulo.pcj.iterators.PCJKeyToJoinBindingSetIterator) List(java.util.List) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Binding(org.openrdf.query.Binding) QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet) BindingSet(org.openrdf.query.BindingSet) HashJoinType(org.apache.rya.accumulo.pcj.iterators.BindingSetHashJoinIterator.HashJoinType) PCJKeyToCrossProductBindingSetIterator(org.apache.rya.accumulo.pcj.iterators.PCJKeyToCrossProductBindingSetIterator) Text(org.apache.hadoop.io.Text) Range(org.apache.accumulo.core.data.Range) QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet) SailException(org.openrdf.sail.SailException) TableNotFoundException(org.apache.accumulo.core.client.TableNotFoundException) PcjException(org.apache.rya.indexing.pcj.storage.PcjException) BindingSetConversionException(org.apache.rya.indexing.pcj.storage.accumulo.BindingSetConverter.BindingSetConversionException) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) AccumuloSecurityException(org.apache.accumulo.core.client.AccumuloSecurityException) NoSuchElementException(java.util.NoSuchElementException) MalformedQueryException(org.openrdf.query.MalformedQueryException) PCJStorageException(org.apache.rya.indexing.pcj.storage.PrecomputedJoinStorage.PCJStorageException) AccumuloException(org.apache.accumulo.core.client.AccumuloException) IteratorWrapper(org.apache.rya.api.utils.IteratorWrapper) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) Value(org.openrdf.model.Value) IteratorCombiner(org.apache.rya.accumulo.pcj.iterators.IteratorCombiner) BindingSetHashJoinIterator(org.apache.rya.accumulo.pcj.iterators.BindingSetHashJoinIterator)

Example 84 with QueryBindingSet

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

the class OneOfVisitorTest method assertBindingSet.

private static void assertBindingSet(final Iterator<BindingSet> bindingSetIter, final Iterator<Resource> expectedValues) {
    while (expectedValues.hasNext()) {
        final Resource expectedValue = expectedValues.next();
        assertTrue(bindingSetIter.hasNext());
        final BindingSet bindingSet = bindingSetIter.next();
        assertTrue(bindingSet instanceof QueryBindingSet);
        assertEquals(1, bindingSet.getBindingNames().size());
        final Binding binding = bindingSet.getBinding("s");
        assertNotNull(binding);
        final Value actualValue = binding.getValue();
        assertEquals(expectedValue, actualValue);
    }
}
Also used : Binding(org.openrdf.query.Binding) QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet) BindingSet(org.openrdf.query.BindingSet) Resource(org.openrdf.model.Resource) Value(org.openrdf.model.Value) QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet)

Example 85 with QueryBindingSet

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

the class OneOfVisitor method meetSP.

@Override
protected void meetSP(final StatementPattern node) throws Exception {
    final Var subVar = node.getSubjectVar();
    final Var predVar = node.getPredicateVar();
    final Var objVar = node.getObjectVar();
    final Var conVar = node.getContextVar();
    if (predVar != null && objVar != null && objVar.getValue() != null && objVar.getValue() instanceof Resource && RDF.TYPE.equals(predVar.getValue()) && !EXPANDED.equals(conVar)) {
        final Resource object = (Resource) objVar.getValue();
        if (inferenceEngine.isEnumeratedType(object)) {
            final Set<BindingSet> solutions = new LinkedHashSet<>();
            final Set<Resource> enumeration = inferenceEngine.getEnumeration(object);
            for (final Resource enumType : enumeration) {
                final QueryBindingSet qbs = new QueryBindingSet();
                qbs.addBinding(subVar.getName(), enumType);
                solutions.add(qbs);
            }
            if (!solutions.isEmpty()) {
                final BindingSetAssignment enumNode = new BindingSetAssignment();
                enumNode.setBindingSets(solutions);
                node.replaceWith(enumNode);
                log.trace("Replacing node with inferred one of enumeration: " + enumNode);
            }
        }
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet) BindingSet(org.openrdf.query.BindingSet) Var(org.openrdf.query.algebra.Var) BindingSetAssignment(org.openrdf.query.algebra.BindingSetAssignment) Resource(org.openrdf.model.Resource) QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet)

Aggregations

QueryBindingSet (org.openrdf.query.algebra.evaluation.QueryBindingSet)107 Test (org.junit.Test)84 BindingSet (org.openrdf.query.BindingSet)79 QueryEvaluationException (org.openrdf.query.QueryEvaluationException)52 RyaURI (org.apache.rya.api.domain.RyaURI)46 RyaStatement (org.apache.rya.api.domain.RyaStatement)45 SPARQLParser (org.openrdf.query.parser.sparql.SPARQLParser)42 StatementPattern (org.openrdf.query.algebra.StatementPattern)41 ParsedQuery (org.openrdf.query.parser.ParsedQuery)41 RyaType (org.apache.rya.api.domain.RyaType)39 LiteralImpl (org.openrdf.model.impl.LiteralImpl)32 ArrayList (java.util.ArrayList)30 HashSet (java.util.HashSet)28 URIImpl (org.openrdf.model.impl.URIImpl)27 StatementMetadata (org.apache.rya.api.domain.StatementMetadata)26 VisibilityBindingSet (org.apache.rya.api.model.VisibilityBindingSet)20 StatementMetadataNode (org.apache.rya.indexing.statement.metadata.matching.StatementMetadataNode)15 Statement (org.openrdf.model.Statement)15 TupleExpr (org.openrdf.query.algebra.TupleExpr)14 Collection (java.util.Collection)13