Search in sources :

Example 11 with Extension

use of org.eclipse.rdf4j.query.algebra.Extension in project rdf4j by eclipse.

the class TupleExprBuilder method processHavingClause.

private TupleExpr processHavingClause(ASTHavingClause havingNode, TupleExpr tupleExpr, Group group) throws VisitorException {
    if (havingNode != null) {
        // create an implicit group
        if (group == null) {
            group = new Group(tupleExpr);
        }
        ValueExpr expr = (ValueExpr) havingNode.jjtGetChild(0).jjtAccept(this, tupleExpr);
        // retrieve any aggregate operators from the expression.
        AggregateCollector collector = new AggregateCollector();
        collector.meetOther(expr);
        // replace operator occurrences with an anonymous var, and alias it
        // to the group
        Extension extension = new Extension();
        for (AggregateOperator operator : collector.getOperators()) {
            Var var = createAnonVar();
            // replace occurrence of the operator in the filter expression
            // with the variable.
            AggregateOperatorReplacer replacer = new AggregateOperatorReplacer(operator, var);
            replacer.meetOther(expr);
            String alias = var.getName();
            // create an extension linking the operator to the variable
            // name.
            ExtensionElem pe = new ExtensionElem(operator, alias);
            extension.addElement(pe);
            // add the aggregate operator to the group.
            GroupElem ge = new GroupElem(alias, operator);
            // FIXME quite often the aggregate in the HAVING clause will be
            // a duplicate of an aggregate in the projection. We could
            // perhaps
            // optimize for that, to avoid having to evaluate twice.
            group.addGroupElement(ge);
        }
        extension.setArg(group);
        tupleExpr = new Filter(extension, expr);
    }
    return tupleExpr;
}
Also used : Extension(org.eclipse.rdf4j.query.algebra.Extension) Group(org.eclipse.rdf4j.query.algebra.Group) ValueExpr(org.eclipse.rdf4j.query.algebra.ValueExpr) Filter(org.eclipse.rdf4j.query.algebra.Filter) Var(org.eclipse.rdf4j.query.algebra.Var) AggregateOperator(org.eclipse.rdf4j.query.algebra.AggregateOperator) GroupElem(org.eclipse.rdf4j.query.algebra.GroupElem) ExtensionElem(org.eclipse.rdf4j.query.algebra.ExtensionElem)

Example 12 with Extension

use of org.eclipse.rdf4j.query.algebra.Extension in project rdf4j by eclipse.

the class AbstractQueryBuilder method projection.

private UnaryTupleOperator projection() {
    if (!mProjectionPatterns.isEmpty()) {
        return multiProjection();
    } else {
        Extension aExt = null;
        ProjectionElemList aList = new ProjectionElemList();
        for (String aVar : mProjectionVars) {
            aList.addElement(new ProjectionElem(aVar));
        }
        Projection aProjection = new Projection();
        aProjection.setProjectionElemList(aList);
        if (aExt != null) {
            aProjection.setArg(aExt);
        }
        return aProjection;
    }
}
Also used : Extension(org.eclipse.rdf4j.query.algebra.Extension) ProjectionElemList(org.eclipse.rdf4j.query.algebra.ProjectionElemList) MultiProjection(org.eclipse.rdf4j.query.algebra.MultiProjection) Projection(org.eclipse.rdf4j.query.algebra.Projection) ProjectionElem(org.eclipse.rdf4j.query.algebra.ProjectionElem)

Example 13 with Extension

use of org.eclipse.rdf4j.query.algebra.Extension in project rdf4j by eclipse.

the class TupleExprBuilder method visit.

@Override
public Object visit(ASTBind node, Object data) throws VisitorException {
    // bind expression
    ValueExpr ve = (ValueExpr) node.jjtGetChild(0).jjtAccept(this, data);
    // name to bind the expression outcome to
    Node aliasNode = node.jjtGetChild(1);
    String alias = ((ASTVar) aliasNode).getName();
    Extension extension = new Extension();
    extension.addElement(new ExtensionElem(ve, alias));
    TupleExpr result = null;
    TupleExpr arg = graphPattern.buildTupleExpr();
    // check if alias is not previously used.
    if (arg.getBindingNames().contains(alias)) {
        // SES-2314 we need to doublecheck that the reused varname is not
        // just
        // for an anonymous var or a constant.
        VarCollector collector = new VarCollector();
        arg.visit(collector);
        for (Var v : collector.getCollectedVars()) {
            if (alias.equals(v.getName())) {
                if (!v.isConstant() && !v.isAnonymous()) {
                    throw new VisitorException(String.format("BIND clause alias '%s' was previously used", alias));
                }
                break;
            }
        }
    }
    if (arg instanceof Filter) {
        result = arg;
        // the BIND expression.
        while (((Filter) arg).getArg() instanceof Filter) {
            arg = ((Filter) arg).getArg();
        }
        extension.setArg(((Filter) arg).getArg());
        ((Filter) arg).setArg(extension);
    } else {
        extension.setArg(arg);
        result = extension;
    }
    GraphPattern replacementGP = new GraphPattern(graphPattern);
    replacementGP.addRequiredTE(result);
    graphPattern = replacementGP;
    return result;
}
Also used : Extension(org.eclipse.rdf4j.query.algebra.Extension) ValueExpr(org.eclipse.rdf4j.query.algebra.ValueExpr) Filter(org.eclipse.rdf4j.query.algebra.Filter) Var(org.eclipse.rdf4j.query.algebra.Var) QueryModelNode(org.eclipse.rdf4j.query.algebra.QueryModelNode) IsBNode(org.eclipse.rdf4j.query.algebra.IsBNode) ExtensionElem(org.eclipse.rdf4j.query.algebra.ExtensionElem) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr)

Aggregations

Extension (org.eclipse.rdf4j.query.algebra.Extension)13 ExtensionElem (org.eclipse.rdf4j.query.algebra.ExtensionElem)11 Var (org.eclipse.rdf4j.query.algebra.Var)10 ProjectionElemList (org.eclipse.rdf4j.query.algebra.ProjectionElemList)9 TupleExpr (org.eclipse.rdf4j.query.algebra.TupleExpr)9 ValueExpr (org.eclipse.rdf4j.query.algebra.ValueExpr)9 Projection (org.eclipse.rdf4j.query.algebra.Projection)8 ProjectionElem (org.eclipse.rdf4j.query.algebra.ProjectionElem)8 MultiProjection (org.eclipse.rdf4j.query.algebra.MultiProjection)7 ArrayList (java.util.ArrayList)5 Reduced (org.eclipse.rdf4j.query.algebra.Reduced)5 Distinct (org.eclipse.rdf4j.query.algebra.Distinct)4 StatementPattern (org.eclipse.rdf4j.query.algebra.StatementPattern)4 ValueConstant (org.eclipse.rdf4j.query.algebra.ValueConstant)4 AggregateOperator (org.eclipse.rdf4j.query.algebra.AggregateOperator)3 BNodeGenerator (org.eclipse.rdf4j.query.algebra.BNodeGenerator)3 EmptySet (org.eclipse.rdf4j.query.algebra.EmptySet)3 Filter (org.eclipse.rdf4j.query.algebra.Filter)3 Group (org.eclipse.rdf4j.query.algebra.Group)3 GroupElem (org.eclipse.rdf4j.query.algebra.GroupElem)3