Search in sources :

Example 1 with OrderElem

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

the class QueryModelBuilder method visit.

@Override
public TupleExpr visit(ASTSelectQuery node, Object data) throws VisitorException {
    TupleExpr tupleExpr;
    ASTQueryBody queryBodyNode = node.getQueryBody();
    if (queryBodyNode != null) {
        // Build tuple expression for query body
        tupleExpr = (TupleExpr) queryBodyNode.jjtAccept(this, null);
    } else {
        tupleExpr = new SingletonSet();
    }
    // Apply result ordering
    ASTOrderBy orderByNode = node.getOrderBy();
    if (orderByNode != null) {
        List<OrderElem> orderElemements = (List<OrderElem>) orderByNode.jjtAccept(this, null);
        tupleExpr = new Order(tupleExpr, orderElemements);
    }
    // Apply projection
    tupleExpr = (TupleExpr) node.getSelectClause().jjtAccept(this, tupleExpr);
    // process limit and offset clauses, if present.
    ASTLimit limitNode = node.getLimit();
    int limit = -1;
    if (limitNode != null) {
        limit = (Integer) limitNode.jjtAccept(this, null);
    }
    ASTOffset offsetNode = node.getOffset();
    int offset = -1;
    if (offsetNode != null) {
        offset = (Integer) offsetNode.jjtAccept(this, null);
    }
    if (offset >= 1 || limit >= 0) {
        tupleExpr = new Slice(tupleExpr, offset, limit);
    }
    return tupleExpr;
}
Also used : Order(org.eclipse.rdf4j.query.algebra.Order) ASTLimit(org.eclipse.rdf4j.query.parser.serql.ast.ASTLimit) SingletonSet(org.eclipse.rdf4j.query.algebra.SingletonSet) Slice(org.eclipse.rdf4j.query.algebra.Slice) ASTQueryBody(org.eclipse.rdf4j.query.parser.serql.ast.ASTQueryBody) OrderElem(org.eclipse.rdf4j.query.algebra.OrderElem) ASTInList(org.eclipse.rdf4j.query.parser.serql.ast.ASTInList) ArrayList(java.util.ArrayList) List(java.util.List) ProjectionElemList(org.eclipse.rdf4j.query.algebra.ProjectionElemList) ASTOffset(org.eclipse.rdf4j.query.parser.serql.ast.ASTOffset) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr) ASTOrderBy(org.eclipse.rdf4j.query.parser.serql.ast.ASTOrderBy)

Example 2 with OrderElem

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

the class QueryModelBuilder method visit.

@Override
public List<OrderElem> visit(ASTOrderBy node, Object data) throws VisitorException {
    List<ASTOrderExpr> orderExprList = node.getOrderExprList();
    List<OrderElem> elements = new ArrayList<OrderElem>(orderExprList.size());
    for (ASTOrderExpr orderExpr : orderExprList) {
        elements.add((OrderElem) orderExpr.jjtAccept(this, null));
    }
    return elements;
}
Also used : ArrayList(java.util.ArrayList) OrderElem(org.eclipse.rdf4j.query.algebra.OrderElem) ASTOrderExpr(org.eclipse.rdf4j.query.parser.serql.ast.ASTOrderExpr)

Example 3 with OrderElem

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

the class SerqlTupleExprRenderer method render.

/**
 * @inheritDoc
 */
public String render(TupleExpr theExpr) throws Exception {
    theExpr.visit(this);
    if (mBuffer.length() > 0) {
        return mBuffer.toString();
    }
    boolean aFirst = true;
    StringBuffer aQuery = new StringBuffer();
    if (!mProjection.isEmpty()) {
        if (isSelect()) {
            aQuery.append("select ");
        } else {
            aQuery.append("construct ");
        }
        if (mDistinct) {
            aQuery.append("distinct ");
        }
        if (mReduced && isSelect()) {
            aQuery.append("reduced ");
        }
        aFirst = true;
        if (!isSelect()) {
            aQuery.append("\n");
        }
        for (ProjectionElemList aList : mProjection) {
            if (isSPOElemList(aList)) {
                if (!aFirst) {
                    aQuery.append(",\n");
                } else {
                    aFirst = false;
                }
                aQuery.append(renderPattern(toStatementPattern(aList)));
            } else {
                for (ProjectionElem aElem : aList.getElements()) {
                    if (!aFirst) {
                        aQuery.append(", ");
                    } else {
                        aFirst = false;
                    }
                    aQuery.append(mExtensions.containsKey(aElem.getSourceName()) ? mValueExprRenderer.render(mExtensions.get(aElem.getSourceName())) : aElem.getSourceName());
                    if (!aElem.getSourceName().equals(aElem.getTargetName()) || (mExtensions.containsKey(aElem.getTargetName()) && !mExtensions.containsKey(aElem.getSourceName()))) {
                        aQuery.append(" as ").append(mExtensions.containsKey(aElem.getTargetName()) ? mValueExprRenderer.render(mExtensions.get(aElem.getTargetName())) : aElem.getTargetName());
                    }
                }
            }
        }
        aQuery.append("\n");
    }
    if (mJoinBuffer.length() > 0) {
        mJoinBuffer.setCharAt(mJoinBuffer.lastIndexOf(","), ' ');
        aQuery.append("from\n");
        aQuery.append(mJoinBuffer);
    }
    if (mFilter != null) {
        aQuery.append("where\n");
        aQuery.append(mValueExprRenderer.render(mFilter));
    }
    if (!mOrdering.isEmpty()) {
        aQuery.append("\norder by ");
        aFirst = true;
        for (OrderElem aOrder : mOrdering) {
            if (!aFirst) {
                aQuery.append(", ");
            } else {
                aFirst = false;
            }
            aQuery.append(aOrder.getExpr());
            aQuery.append(" ");
            if (aOrder.isAscending()) {
                aQuery.append("asc");
            } else {
                aQuery.append("desc");
            }
        }
    }
    if (mLimit != -1) {
        aQuery.append("\nlimit ").append(mLimit);
    }
    if (mOffset != -1) {
        aQuery.append("\noffset ").append(mOffset);
    }
    return aQuery.toString();
}
Also used : ProjectionElemList(org.eclipse.rdf4j.query.algebra.ProjectionElemList) OrderElem(org.eclipse.rdf4j.query.algebra.OrderElem) ProjectionElem(org.eclipse.rdf4j.query.algebra.ProjectionElem)

Example 4 with OrderElem

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

the class QueryModelBuilder method visit.

@Override
public TupleExpr visit(ASTConstructQuery node, Object data) throws VisitorException {
    TupleExpr tupleExpr;
    if (node.hasQueryBody()) {
        // Build tuple expression for query body
        tupleExpr = (TupleExpr) node.getQueryBody().jjtAccept(this, null);
    } else {
        tupleExpr = new SingletonSet();
    }
    // Apply result ordering
    ASTOrderBy orderByNode = node.getOrderBy();
    if (orderByNode != null) {
        List<OrderElem> orderElemements = (List<OrderElem>) orderByNode.jjtAccept(this, null);
        tupleExpr = new Order(tupleExpr, orderElemements);
    }
    // Create constructor
    ConstructorBuilder cb = new ConstructorBuilder();
    ASTConstruct constructNode = node.getConstructClause();
    if (!constructNode.isWildcard()) {
        TupleExpr constructExpr = (TupleExpr) constructNode.jjtAccept(this, null);
        tupleExpr = cb.buildConstructor(tupleExpr, constructExpr, constructNode.isDistinct(), constructNode.isReduced());
    } else if (node.hasQueryBody()) {
        tupleExpr = cb.buildConstructor(tupleExpr, constructNode.isDistinct(), constructNode.isReduced());
    }
    // else: "construct *" without query body, just return the SingletonSet
    // process limit and offset clauses, if present.
    ASTLimit limitNode = node.getLimit();
    int limit = -1;
    if (limitNode != null) {
        limit = (Integer) limitNode.jjtAccept(this, null);
    }
    ASTOffset offsetNode = node.getOffset();
    int offset = -1;
    if (offsetNode != null) {
        offset = (Integer) offsetNode.jjtAccept(this, null);
    }
    if (offset >= 1 || limit >= 0) {
        tupleExpr = new Slice(tupleExpr, offset, limit);
    }
    return tupleExpr;
}
Also used : Order(org.eclipse.rdf4j.query.algebra.Order) SingletonSet(org.eclipse.rdf4j.query.algebra.SingletonSet) OrderElem(org.eclipse.rdf4j.query.algebra.OrderElem) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr) ASTLimit(org.eclipse.rdf4j.query.parser.serql.ast.ASTLimit) Slice(org.eclipse.rdf4j.query.algebra.Slice) ASTInList(org.eclipse.rdf4j.query.parser.serql.ast.ASTInList) ArrayList(java.util.ArrayList) List(java.util.List) ProjectionElemList(org.eclipse.rdf4j.query.algebra.ProjectionElemList) ASTOffset(org.eclipse.rdf4j.query.parser.serql.ast.ASTOffset) ASTConstruct(org.eclipse.rdf4j.query.parser.serql.ast.ASTConstruct) ASTOrderBy(org.eclipse.rdf4j.query.parser.serql.ast.ASTOrderBy)

Example 5 with OrderElem

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

the class TupleExprBuilder method processOrderClause.

private TupleExpr processOrderClause(ASTOrderClause orderNode, TupleExpr tupleExpr, Group group) throws VisitorException {
    if (orderNode != null) {
        @SuppressWarnings("unchecked") List<OrderElem> orderElements = (List<OrderElem>) orderNode.jjtAccept(this, null);
        for (OrderElem orderElem : orderElements) {
            // retrieve any aggregate operators from the order element.
            AggregateCollector collector = new AggregateCollector();
            collector.meet(orderElem);
            Extension extension = new Extension();
            for (AggregateOperator operator : collector.getOperators()) {
                Var var = createAnonVar();
                // replace occurrence of the operator in the order condition
                // with the variable.
                AggregateOperatorReplacer replacer = new AggregateOperatorReplacer(operator, var);
                replacer.meet(orderElem);
                // create an extension linking the operator to the variable
                // name.
                String alias = var.getName();
                ExtensionElem pe = new ExtensionElem(operator, alias);
                extension.addElement(pe);
                // add the aggregate operator to the group.
                GroupElem ge = new GroupElem(alias, operator);
                group.addGroupElement(ge);
            }
            if (!extension.getElements().isEmpty()) {
                extension.setArg(tupleExpr);
                tupleExpr = extension;
            }
        }
        tupleExpr = new Order(tupleExpr, orderElements);
    }
    return tupleExpr;
}
Also used : Order(org.eclipse.rdf4j.query.algebra.Order) Var(org.eclipse.rdf4j.query.algebra.Var) GroupElem(org.eclipse.rdf4j.query.algebra.GroupElem) OrderElem(org.eclipse.rdf4j.query.algebra.OrderElem) ExtensionElem(org.eclipse.rdf4j.query.algebra.ExtensionElem) Extension(org.eclipse.rdf4j.query.algebra.Extension) AggregateOperator(org.eclipse.rdf4j.query.algebra.AggregateOperator) List(java.util.List) ArrayList(java.util.ArrayList) ProjectionElemList(org.eclipse.rdf4j.query.algebra.ProjectionElemList)

Aggregations

OrderElem (org.eclipse.rdf4j.query.algebra.OrderElem)6 ProjectionElemList (org.eclipse.rdf4j.query.algebra.ProjectionElemList)5 ArrayList (java.util.ArrayList)4 List (java.util.List)3 Order (org.eclipse.rdf4j.query.algebra.Order)3 ProjectionElem (org.eclipse.rdf4j.query.algebra.ProjectionElem)2 SingletonSet (org.eclipse.rdf4j.query.algebra.SingletonSet)2 Slice (org.eclipse.rdf4j.query.algebra.Slice)2 TupleExpr (org.eclipse.rdf4j.query.algebra.TupleExpr)2 ASTInList (org.eclipse.rdf4j.query.parser.serql.ast.ASTInList)2 ASTLimit (org.eclipse.rdf4j.query.parser.serql.ast.ASTLimit)2 ASTOffset (org.eclipse.rdf4j.query.parser.serql.ast.ASTOffset)2 ASTOrderBy (org.eclipse.rdf4j.query.parser.serql.ast.ASTOrderBy)2 IRI (org.eclipse.rdf4j.model.IRI)1 AggregateOperator (org.eclipse.rdf4j.query.algebra.AggregateOperator)1 Extension (org.eclipse.rdf4j.query.algebra.Extension)1 ExtensionElem (org.eclipse.rdf4j.query.algebra.ExtensionElem)1 GroupElem (org.eclipse.rdf4j.query.algebra.GroupElem)1 Var (org.eclipse.rdf4j.query.algebra.Var)1 ParsedBooleanQuery (org.eclipse.rdf4j.query.parser.ParsedBooleanQuery)1