Search in sources :

Example 1 with ASTProjectionElem

use of org.eclipse.rdf4j.query.parser.serql.ast.ASTProjectionElem in project rdf4j by eclipse.

the class ProjectionProcessor method visit.

@Override
public Object visit(ASTSelect selectNode, Object data) throws VisitorException {
    // Collect all variables used in the body of the select query
    Set<String> bodyVars = VariableCollector.process(selectNode.jjtGetParent());
    if (selectNode.isWildcard()) {
        // Make wildcard projection explicit
        for (String varName : bodyVars) {
            ASTProjectionElem projElemNode = new ASTProjectionElem(SyntaxTreeBuilderTreeConstants.JJTPROJECTIONELEM);
            selectNode.jjtAppendChild(projElemNode);
            projElemNode.jjtSetParent(selectNode);
            ASTVar varNode = new ASTVar(SyntaxTreeBuilderTreeConstants.JJTVAR);
            varNode.setName(varName);
            projElemNode.jjtAppendChild(varNode);
            varNode.jjtSetParent(projElemNode);
        }
        selectNode.setWildcard(false);
    } else {
        // Verify that all projection vars are bound
        Set<String> projVars = new LinkedHashSet<String>();
        for (ASTProjectionElem projElem : selectNode.getProjectionElemList()) {
            projVars.addAll(VariableCollector.process(projElem.getValueExpr()));
        }
        projVars.removeAll(bodyVars);
        if (!projVars.isEmpty()) {
            StringBuilder errMsg = new StringBuilder(64);
            errMsg.append("Unbound variable(s) in projection: ");
            Iterator<String> iter = projVars.iterator();
            while (iter.hasNext()) {
                errMsg.append(iter.next());
                if (iter.hasNext()) {
                    errMsg.append(", ");
                }
            }
            throw new VisitorException(errMsg.toString());
        }
    }
    return data;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ASTVar(org.eclipse.rdf4j.query.parser.serql.ast.ASTVar) ASTProjectionElem(org.eclipse.rdf4j.query.parser.serql.ast.ASTProjectionElem) VisitorException(org.eclipse.rdf4j.query.parser.serql.ast.VisitorException)

Example 2 with ASTProjectionElem

use of org.eclipse.rdf4j.query.parser.serql.ast.ASTProjectionElem in project rdf4j by eclipse.

the class ProjectionAliasProcessor method visit.

@Override
public Object visit(ASTSelect node, Object data) throws VisitorException {
    // Iterate over all projection elements to retrieve the defined aliases
    Set<String> aliases = new HashSet<String>();
    List<Node> unaliasedNodes = new ArrayList<Node>();
    for (int i = 0; i < node.jjtGetNumChildren(); i++) {
        ASTProjectionElem projElem = (ASTProjectionElem) node.jjtGetChild(i);
        String alias = projElem.getAlias();
        if (alias == null && projElem.getValueExpr() instanceof ASTVar) {
            alias = ((ASTVar) projElem.getValueExpr()).getName();
        }
        if (alias != null) {
            boolean isUnique = aliases.add(alias);
            if (!isUnique) {
                throw new VisitorException("Duplicate projection element names: '" + alias + "'");
            }
        } else {
            unaliasedNodes.add(projElem);
        }
    }
    // Iterate over the unaliased nodes and generate aliases for them
    int aliasNo = 1;
    for (Node projElem : unaliasedNodes) {
        // Generate unique alias for projection element
        String alias;
        while (aliases.contains(alias = "_" + aliasNo++)) {
        // try again
        }
        aliases.add(alias);
        ASTString aliasNode = new ASTString(SyntaxTreeBuilderTreeConstants.JJTSTRING);
        aliasNode.setValue(alias);
        aliasNode.jjtSetParent(projElem);
        projElem.jjtAppendChild(aliasNode);
    }
    return data;
}
Also used : ASTVar(org.eclipse.rdf4j.query.parser.serql.ast.ASTVar) ASTString(org.eclipse.rdf4j.query.parser.serql.ast.ASTString) Node(org.eclipse.rdf4j.query.parser.serql.ast.Node) ArrayList(java.util.ArrayList) ASTString(org.eclipse.rdf4j.query.parser.serql.ast.ASTString) ASTProjectionElem(org.eclipse.rdf4j.query.parser.serql.ast.ASTProjectionElem) VisitorException(org.eclipse.rdf4j.query.parser.serql.ast.VisitorException) HashSet(java.util.HashSet)

Example 3 with ASTProjectionElem

use of org.eclipse.rdf4j.query.parser.serql.ast.ASTProjectionElem in project rdf4j by eclipse.

the class QueryModelBuilder method visit.

@Override
public TupleExpr visit(ASTSelect node, Object data) throws VisitorException {
    TupleExpr result = (TupleExpr) data;
    Extension extension = new Extension();
    ProjectionElemList projElemList = new ProjectionElemList();
    for (ASTProjectionElem projElemNode : node.getProjectionElemList()) {
        ValueExpr valueExpr = (ValueExpr) projElemNode.getValueExpr().jjtAccept(this, null);
        String alias = projElemNode.getAlias();
        if (alias != null) {
            // aliased projection element
            extension.addElement(new ExtensionElem(valueExpr, alias));
            projElemList.addElement(new ProjectionElem(alias));
        } else if (valueExpr instanceof Var) {
            // unaliased variable
            Var projVar = (Var) valueExpr;
            projElemList.addElement(new ProjectionElem(projVar.getName()));
        } else {
            throw new IllegalStateException("required alias for non-Var projection elements not found");
        }
    }
    if (!extension.getElements().isEmpty()) {
        extension.setArg(result);
        result = extension;
    }
    result = new Projection(result, projElemList);
    if (node.isDistinct()) {
        result = new Distinct(result);
    } else if (node.isReduced()) {
        result = new Reduced(result);
    }
    return result;
}
Also used : ProjectionElemList(org.eclipse.rdf4j.query.algebra.ProjectionElemList) ValueExpr(org.eclipse.rdf4j.query.algebra.ValueExpr) ASTValueExpr(org.eclipse.rdf4j.query.parser.serql.ast.ASTValueExpr) ASTVar(org.eclipse.rdf4j.query.parser.serql.ast.ASTVar) Var(org.eclipse.rdf4j.query.algebra.Var) ExtensionElem(org.eclipse.rdf4j.query.algebra.ExtensionElem) Projection(org.eclipse.rdf4j.query.algebra.Projection) ASTString(org.eclipse.rdf4j.query.parser.serql.ast.ASTString) ASTProjectionElem(org.eclipse.rdf4j.query.parser.serql.ast.ASTProjectionElem) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr) Reduced(org.eclipse.rdf4j.query.algebra.Reduced) Extension(org.eclipse.rdf4j.query.algebra.Extension) Distinct(org.eclipse.rdf4j.query.algebra.Distinct) ProjectionElem(org.eclipse.rdf4j.query.algebra.ProjectionElem) ASTProjectionElem(org.eclipse.rdf4j.query.parser.serql.ast.ASTProjectionElem)

Aggregations

ASTProjectionElem (org.eclipse.rdf4j.query.parser.serql.ast.ASTProjectionElem)3 ASTVar (org.eclipse.rdf4j.query.parser.serql.ast.ASTVar)3 ASTString (org.eclipse.rdf4j.query.parser.serql.ast.ASTString)2 VisitorException (org.eclipse.rdf4j.query.parser.serql.ast.VisitorException)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 Distinct (org.eclipse.rdf4j.query.algebra.Distinct)1 Extension (org.eclipse.rdf4j.query.algebra.Extension)1 ExtensionElem (org.eclipse.rdf4j.query.algebra.ExtensionElem)1 Projection (org.eclipse.rdf4j.query.algebra.Projection)1 ProjectionElem (org.eclipse.rdf4j.query.algebra.ProjectionElem)1 ProjectionElemList (org.eclipse.rdf4j.query.algebra.ProjectionElemList)1 Reduced (org.eclipse.rdf4j.query.algebra.Reduced)1 TupleExpr (org.eclipse.rdf4j.query.algebra.TupleExpr)1 ValueExpr (org.eclipse.rdf4j.query.algebra.ValueExpr)1 Var (org.eclipse.rdf4j.query.algebra.Var)1 ASTValueExpr (org.eclipse.rdf4j.query.parser.serql.ast.ASTValueExpr)1 Node (org.eclipse.rdf4j.query.parser.serql.ast.Node)1