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