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