if (graphConstraint != null && graphConstraint.isVariable()
&& !(node instanceof ASTSelectQuery && ((ASTSelectQuery)node).isSelectAll())) {
boolean graphVariableIsSelected = false;
for (int i = 0; i < node.jjtGetNumChildren(); i++) {
final Node n = node.jjtGetChild(i);
if (n instanceof ASTVar) {
final ASTVar variable = (ASTVar) n;
if (variable.getName().equals(graphConstraint.getName())) {
graphVariableIsSelected = true;
}
}
}
// as a workaround we rename the graphvariable in the subquery
if (!graphVariableIsSelected) {
int index=0;
do {
graphConstraint = new Variable(graphConstraint.getName() + index);
index++;
} while (this.hasThisVariable(node, graphConstraint));
}
}
final int numberChildren = node.jjtGetNumChildren();
boolean onlyAggregations = true;
// insert limit operator
for (int i = 0; i < numberChildren; i++) {
if (node.jjtGetChild(i) instanceof ASTLimit) {
node.jjtGetChild(i).accept(this, connection);
}
}
// insert offset operator
for (int i = 0; i < numberChildren; i++) {
if (node.jjtGetChild(i) instanceof ASTOffset) {
node.jjtGetChild(i).accept(this, connection);
}
}
if (node instanceof ASTSelectQuery && ((ASTSelectQuery)node).isDistinct()) {
// or insert a DISTINCT operator into the operator graph:
connection.connectAndSetAsNewOperatorConnection(new Distinct());
}
LinkedList<AddComputedBinding> listOACB = new LinkedList<AddComputedBinding>();
boolean group = false;
for (int i = 0; i < numberChildren; i++) {
final Node childi = node.jjtGetChild(i);
if (childi instanceof ASTGroup) {
group = true;
}
}
// insert projection operator
if (!(node instanceof ASTSelectQuery && ((ASTSelectQuery)node).isSelectAll())) {
final Projection p = new Projection();
final LinkedList<AddComputedBinding> listOfAddComputedBindings = new LinkedList<AddComputedBinding>();
for (int i = 0; i < numberChildren; i++) {
if (node.jjtGetChild(i) instanceof ASTVar) {
final ASTVar variable = (ASTVar) node.jjtGetChild(i);
p.addProjectionElement(new Variable(variable.getName()));
onlyAggregations = false;
} else if (node.jjtGetChild(i) instanceof ASTAs) {
final ASTVar variable = (ASTVar) node.jjtGetChild(i)
.jjtGetChild(1);
final lupos.sparql1_1.Node constraint = node.jjtGetChild(i)
.jjtGetChild(0);
/*
* Detecting Errors in SelectQuery if aggregations are used
* and additional variables are not bound by a GROUP BY
* statement
*/
// this.prooveBoundedGroup(node.jjtGetChild(i));
if (!(constraint instanceof ASTAggregation)) {
onlyAggregations = false;
}
final Variable var2 = new Variable(variable.getName());
p.addProjectionElement(var2);
final AddComputedBinding acb = group ? new GroupByAddComputedBinding()
: new AddComputedBinding();
acb.addProjectionElement(var2, constraint);
listOfAddComputedBindings.add(acb);
}
}
// deleting of values if there is only an aggregation statement
if (onlyAggregations || group) {
connection.connectAndSetAsNewOperatorConnection(new Distinct());
}
listOACB = this.topologicalSorting(listOfAddComputedBindings);
connection.connectAndSetAsNewOperatorConnection(p);
}
// insert sort operator
for (int i = 0; i < numberChildren; i++) {
if (node.jjtGetChild(i) instanceof ASTOrderConditions) {
node.jjtGetChild(i).accept(this, connection);
}
}
for (final AddComputedBinding acb : listOACB) {
connection.connectAndSetAsNewOperatorConnection(acb);
}
// Dealing with the HAVING clause
for (int i = 0; i < numberChildren; i++) {
final Node childi = node.jjtGetChild(i);
if (childi instanceof ASTHaving) {
for (int k = 0; k < childi.jjtGetNumChildren(); k++) {
if (childi.jjtGetChild(k) instanceof ASTFilterConstraint) {
final Having filter = new Having((ASTFilterConstraint) childi
.jjtGetChild(k));
this.processExistChildren(node, graphConstraint, filter);
filter.setEvaluator(this.evaluator);
connection.connectAndSetAsNewOperatorConnection(filter);
}
}
}
}
// Dealing with the GROUP clause
for (int j = 0; j < numberChildren; j++) {
final Projection p = new Projection();
final LinkedList<AddComputedBinding> listOfAddComputedBindings = new LinkedList<AddComputedBinding>();
ASTVar variable = null;
final Node childi = node.jjtGetChild(j);
onlyAggregations = true;
if (childi instanceof ASTGroup) {
for (int i = 0; i < childi.jjtGetNumChildren(); i++) {
if (childi.jjtGetChild(i) instanceof ASTAdditionNode
|| childi.jjtGetChild(i) instanceof ASTSubtractionNode
|| childi.jjtGetChild(i) instanceof ASTMultiplicationNode
|| childi.jjtGetChild(i) instanceof ASTDivisionNode) {
throw new Error(
"Error in GROUP BY statement: AS not found");
} else if (childi.jjtGetChild(i) instanceof ASTAs) {
variable = (ASTVar) childi.jjtGetChild(i).jjtGetChild(1);
final lupos.sparql1_1.Node constraint = childi.jjtGetChild(i).jjtGetChild(0);
/*
* Detecting Errors in SelectQuery if aggregations are
* used and additional variables are not bound by a
* GROUP BY statement