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;
}
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;
}
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;
}
Aggregations