Search in sources :

Example 1 with IVariable

use of com.bigdata.bop.IVariable in project wikidata-query-rdf by wikimedia.

the class EmptyLabelServiceOptimizer method optimizeJoinGroup.

@Override
protected void optimizeJoinGroup(AST2BOpContext ctx, StaticAnalysis sa, IBindingSet[] bSets, JoinGroupNode op) {
    final QueryRoot root = sa.getQueryRoot();
    if (root.getQueryType() == QueryType.ASK) {
        return;
    }
    if (root.getWhereClause() == op) {
        op.setProperty(LABEL_SERVICE_PROJECTION, root.getProjection());
    }
    op.getChildren(SubqueryBase.class).forEach(node -> {
        if (node.getWhereClause() != null) {
            BOp whereClause = node.getWhereClause();
            whereClause.setProperty(LABEL_SERVICE_PROJECTION, node.getProjection());
        }
    });
    // Prepare a set of vars, which might be bound both outside of the service and by LabelService
    // Fix for the issue: https://phabricator.wikimedia.org/T159723
    // See also patch for the com.bigdata.rdf.sparql.ast.eval.AST2BOpUtility.addServiceCall()
    Set<IVariable<?>> uncertainVars = collectUncertainVars(sa, bSets, op);
    getLabelServiceNodes(op).forEach(service -> {
        service.setUncertainVars(uncertainVars);
        JoinGroupNode g = (JoinGroupNode) service.getGraphPattern();
        boolean foundArg = false;
        for (BOp st : g.args()) {
            StatementPatternNode sn = (StatementPatternNode) st;
            if (sn.s().isConstant() && BD.SERVICE_PARAM.equals(sn.s().getValue())) {
                continue;
            }
            foundArg = true;
            break;
        }
        if (restoreExtracted(service)) {
            foundArg = true;
        }
        if (!foundArg) {
            addResolutions(ctx, g, getProjectionNode(service));
        }
    });
}
Also used : BOp(com.bigdata.bop.BOp) QueryRoot(com.bigdata.rdf.sparql.ast.QueryRoot) IVariable(com.bigdata.bop.IVariable) SubqueryBase(com.bigdata.rdf.sparql.ast.SubqueryBase) JoinGroupNode(com.bigdata.rdf.sparql.ast.JoinGroupNode) StatementPatternNode(com.bigdata.rdf.sparql.ast.StatementPatternNode)

Example 2 with IVariable

use of com.bigdata.bop.IVariable in project wikidata-query-rdf by wikimedia.

the class WikidataServicePlacementOptimizer method findLatestPossiblePositionForTheServiceNode.

private int findLatestPossiblePositionForTheServiceNode(StaticAnalysis sa, BOp serviceNode, final JoinGroupNode joinGroup) {
    int lastJoinIndex = -1;
    // Retrieve inVars from annotations. If no WIKIDATA_SERVICE_IN_VARS annotation provided
    // (which occurs on first run of LabelServicePlacementOptimizer), we still need to
    // traverse the tree, as there might be NamedSubqueryInclude, which might
    // be producing variables for ServiceNode.
    Object inVarsObject = serviceNode.annotations().get(WIKIDATA_SERVICE_IN_VARS);
    @SuppressWarnings("unchecked") Set<IVariable<?>> inVars = inVarsObject instanceof Set ? (Set<IVariable<?>>) inVarsObject : Collections.emptySet();
    for (int i = joinGroup.size() - 1; i >= 0; i--) {
        BOp child = joinGroup.get(i);
        if (child != serviceNode && child instanceof IBindingProducerNode) {
            // will touch any inbound var for the service
            if (checkIfNodeProducesVars(sa, child, inVars)) {
                lastJoinIndex = i;
                break;
            }
        }
    }
    return lastJoinIndex;
}
Also used : BOp(com.bigdata.bop.BOp) Set(java.util.Set) IBindingSet(com.bigdata.bop.IBindingSet) IVariable(com.bigdata.bop.IVariable) IBindingProducerNode(com.bigdata.rdf.sparql.ast.IBindingProducerNode)

Example 3 with IVariable

use of com.bigdata.bop.IVariable in project wikidata-query-rdf by wikimedia.

the class MWApiServiceFactory method getRequiredBound.

@Override
public Set<IVariable<?>> getRequiredBound(final ServiceNode serviceNode) {
    ServiceParams params = serviceParamsFromNode(serviceNode);
    ApiTemplate api = getServiceTemplate(params);
    Map<String, IVariableOrConstant> potentialVars = api.getInputVars(params);
    // Extract params that have variables linked to them
    return potentialVars.entrySet().stream().filter(entry -> entry.getValue() != null && entry.getValue().isVar()).map(entry -> (IVariable<?>) entry.getValue()).collect(ImmutableSet.toImmutableSet());
}
Also used : IVariableOrConstant(com.bigdata.bop.IVariableOrConstant) URIImpl(org.openrdf.model.impl.URIImpl) Ontology(org.wikidata.query.rdf.common.uri.Ontology) LoggerFactory(org.slf4j.LoggerFactory) AbstractServiceFactory(com.bigdata.rdf.sparql.ast.eval.AbstractServiceFactory) BigdataServiceCall(com.bigdata.rdf.sparql.ast.service.BigdataServiceCall) IVariableOrConstant(com.bigdata.bop.IVariableOrConstant) BigdataNativeServiceOptions(com.bigdata.rdf.sparql.ast.service.BigdataNativeServiceOptions) Objects.requireNonNull(java.util.Objects.requireNonNull) Map(java.util.Map) URI(org.openrdf.model.URI) Path(java.nio.file.Path) GraphPatternGroup(com.bigdata.rdf.sparql.ast.GraphPatternGroup) StatementPatternNode(com.bigdata.rdf.sparql.ast.StatementPatternNode) Logger(org.slf4j.Logger) ImmutableSet(com.google.common.collect.ImmutableSet) MalformedURLException(java.net.MalformedURLException) Files(java.nio.file.Files) ServiceRegistry(com.bigdata.rdf.sparql.ast.service.ServiceRegistry) Set(java.util.Set) IOException(java.io.IOException) Reader(java.io.Reader) ServiceParams(com.bigdata.rdf.sparql.ast.eval.ServiceParams) IGroupMemberNode(com.bigdata.rdf.sparql.ast.IGroupMemberNode) StandardCharsets(java.nio.charset.StandardCharsets) BD(com.bigdata.rdf.store.BD) ServiceNode(com.bigdata.rdf.sparql.ast.service.ServiceNode) Paths(java.nio.file.Paths) TermNode(com.bigdata.rdf.sparql.ast.TermNode) IVariable(com.bigdata.bop.IVariable) Timer(com.codahale.metrics.Timer) Mediawiki(org.wikidata.query.rdf.common.uri.Mediawiki) IServiceOptions(com.bigdata.rdf.sparql.ast.service.IServiceOptions) ServiceCallCreateParams(com.bigdata.rdf.sparql.ast.service.ServiceCallCreateParams) ServiceParams(com.bigdata.rdf.sparql.ast.eval.ServiceParams) IVariable(com.bigdata.bop.IVariable)

Example 4 with IVariable

use of com.bigdata.bop.IVariable in project wikidata-query-rdf by wikimedia.

the class GeoService method getRequiredBound.

@Override
public Set<IVariable<?>> getRequiredBound(final ServiceNode serviceNode) {
    /**
     * This method extracts exactly those variables that are incoming,
     * i.e. must be bound before executing the execution of the service.
     *
     * Those can be only in service parameters.
     */
    final Set<IVariable<?>> requiredBound = new HashSet<>();
    for (StatementPatternNode sp : getStatementPatterns(serviceNode)) {
        final TermNode subj = sp.s();
        final IVariableOrConstant<?> object = sp.o().getValueExpression();
        if (subj.isConstant() && BD.SERVICE_PARAM.equals(subj.getValue()) && object instanceof IVariable<?>) {
            // the subject var is what we return
            requiredBound.add((IVariable<?>) object);
        }
    }
    return requiredBound;
}
Also used : IVariable(com.bigdata.bop.IVariable) StatementPatternNode(com.bigdata.rdf.sparql.ast.StatementPatternNode) TermNode(com.bigdata.rdf.sparql.ast.TermNode) HashSet(java.util.HashSet)

Example 5 with IVariable

use of com.bigdata.bop.IVariable in project wikidata-query-rdf by wikimedia.

the class LabelServiceUnitTest method desiredVars.

@SuppressWarnings("unchecked")
@Test
public void desiredVars() {
    JoinGroupNode patterns = new JoinGroupNode();
    // Label
    patterns.addArg(new StatementPatternNode(new VarNode("item"), createURI(RDFS.LABEL), new VarNode("itemLabel")));
    // Description
    patterns.addArg(new StatementPatternNode(new VarNode("item2"), createURI(SchemaDotOrg.DESCRIPTION), new VarNode("itemDesc")));
    // Fixed name
    patterns.addArg(new StatementPatternNode(createURI(uris().entityIdToURI("Q123")), createURI(RDFS.LABEL), new VarNode("qLabel")));
    // Parameters
    patterns.addArg(new StatementPatternNode(createURI(BD.SERVICE_PARAM), createURI(LabelService.LANGUAGE_PARAM), createConstant("en,fr")));
    ServiceNode serviceNode = new ServiceNode(createURI(LabelService.SERVICE_KEY), patterns);
    final LabelService service = new LabelService();
    Set<IVariable<?>> vars = service.getDesiredBound(serviceNode);
    assertThat(vars, hasSize(2));
    assertThat(vars, hasItems(equalTo(Var.var("item")), equalTo(Var.var("item2"))));
}
Also used : VarNode(com.bigdata.rdf.sparql.ast.VarNode) ServiceNode(com.bigdata.rdf.sparql.ast.service.ServiceNode) IVariable(com.bigdata.bop.IVariable) JoinGroupNode(com.bigdata.rdf.sparql.ast.JoinGroupNode) StatementPatternNode(com.bigdata.rdf.sparql.ast.StatementPatternNode) Test(org.junit.Test)

Aggregations

IVariable (com.bigdata.bop.IVariable)8 StatementPatternNode (com.bigdata.rdf.sparql.ast.StatementPatternNode)5 HashSet (java.util.HashSet)3 BOp (com.bigdata.bop.BOp)2 IBindingSet (com.bigdata.bop.IBindingSet)2 IGroupMemberNode (com.bigdata.rdf.sparql.ast.IGroupMemberNode)2 JoinGroupNode (com.bigdata.rdf.sparql.ast.JoinGroupNode)2 TermNode (com.bigdata.rdf.sparql.ast.TermNode)2 ServiceNode (com.bigdata.rdf.sparql.ast.service.ServiceNode)2 Set (java.util.Set)2 IVariableOrConstant (com.bigdata.bop.IVariableOrConstant)1 IV (com.bigdata.rdf.internal.IV)1 GraphPatternGroup (com.bigdata.rdf.sparql.ast.GraphPatternGroup)1 IBindingProducerNode (com.bigdata.rdf.sparql.ast.IBindingProducerNode)1 QueryRoot (com.bigdata.rdf.sparql.ast.QueryRoot)1 SubqueryBase (com.bigdata.rdf.sparql.ast.SubqueryBase)1 VarNode (com.bigdata.rdf.sparql.ast.VarNode)1 AbstractServiceFactory (com.bigdata.rdf.sparql.ast.eval.AbstractServiceFactory)1 ServiceParams (com.bigdata.rdf.sparql.ast.eval.ServiceParams)1 BigdataNativeServiceOptions (com.bigdata.rdf.sparql.ast.service.BigdataNativeServiceOptions)1