case FOR_CLAUSE: {
ForClauseNode fcNode = (ForClauseNode) cNode;
for (ForVarDeclNode fvdNode : fcNode.getVariables()) {
ILogicalExpression seq = vre(translateExpression(fvdNode.getSequence(), tCtx));
tCtx.pushVariableScope();
LogicalVariable forLVar = newLogicalVariable();
LogicalVariable posLVar = fvdNode.getPosVar() != null ? newLogicalVariable() : null;
UnnestOperator unnest = new UnnestOperator(forLVar,
mutable(ufce(BuiltinOperators.ITERATE, seq)), posLVar, null);
SequenceType forVarType = SequenceType.create(AnyItemType.INSTANCE, Quantifier.QUANT_ONE);
if (fvdNode.getType() != null) {
forVarType = createSequenceType(fvdNode.getType());
}
XQueryVariable forVar = new XQueryVariable(createQName(fvdNode.getForVar()), forVarType,
forLVar);
tCtx.varScope.registerVariable(forVar);
XQueryVariable posVar = null;
if (fvdNode.getPosVar() != null) {
posVar = new XQueryVariable(createQName(fvdNode.getPosVar()), SequenceType.create(
BuiltinTypeRegistry.XS_INTEGER, Quantifier.QUANT_ONE), posLVar);
tCtx.varScope.registerVariable(posVar);
}
assert fvdNode.getScoreVar() == null;
unnest.getInputs().add(mutable(tCtx.op));
tCtx.op = unnest;
++pushCount;
}
break;
}
case LET_CLAUSE: {
LetClauseNode lcNode = (LetClauseNode) cNode;
for (LetVarDeclNode lvdNode : lcNode.getVariables()) {
LogicalVariable seqVar = translateExpression(lvdNode.getSequence(), tCtx);
tCtx.pushVariableScope();
SequenceType letVarType = SequenceType.create(AnyItemType.INSTANCE, Quantifier.QUANT_ONE);
if (lvdNode.getType() != null) {
letVarType = createSequenceType(lvdNode.getType());
}
XQueryVariable letVar = new XQueryVariable(createQName(lvdNode.getLetVar()), letVarType, seqVar);
tCtx.varScope.registerVariable(letVar);
++pushCount;
}
break;
}
case WHERE_CLAUSE: {
WhereClauseNode wcNode = (WhereClauseNode) cNode;
ILogicalExpression condExpr = sfce(BuiltinFunctions.FN_BOOLEAN_1,
vre(translateExpression(wcNode.getCondition(), tCtx)));
SelectOperator select = new SelectOperator(mutable(condExpr));
select.getInputs().add(mutable(tCtx.op));
tCtx.op = select;
break;
}
case ORDERBY_CLAUSE: {
OrderbyClauseNode ocNode = (OrderbyClauseNode) cNode;
List<edu.uci.ics.hyracks.algebricks.common.utils.Pair<OrderOperator.IOrder, Mutable<ILogicalExpression>>> oExprs = new ArrayList<edu.uci.ics.hyracks.algebricks.common.utils.Pair<OrderOperator.IOrder, Mutable<ILogicalExpression>>>();
List<String> collations = new ArrayList<String>();
for (OrderSpecNode osNode : ocNode.getOrderSpec()) {
ILogicalExpression oExpr = vre(translateExpression(osNode.getExpression(), tCtx));
OrderOperator.IOrder o = OrderOperator.ASC_ORDER;
XQueryConstants.OrderDirection oDir = osNode.getDirection();
if (oDir != null) {
switch (oDir) {
case ASCENDING:
o = OrderOperator.ASC_ORDER;
break;
case DESCENDING:
o = OrderOperator.DESC_ORDER;
break;
}
}
/*
StaticContext.EmptyOrderProperty eoProp = osNode.getEmptyOrder();
if (eoProp != null) {
switch (osNode.getEmptyOrder()) {
case GREATEST:
eOrders.add(FLWORExpression.EmptyOrder.GREATEST);
break;
case LEAST:
eOrders.add(FLWORExpression.EmptyOrder.LEAST);
break;
}
} else {
eOrders.add(FLWORExpression.EmptyOrder.DEFAULT);
}
collations.add(osNode.getCollation());
*/
oExprs.add(new edu.uci.ics.hyracks.algebricks.common.utils.Pair<OrderOperator.IOrder, Mutable<ILogicalExpression>>(
o, mutable(oExpr)));
}
OrderOperator order = new OrderOperator(oExprs);
order.getInputs().add(mutable(tCtx.op));
tCtx.op = order;
break;
}
default:
throw new IllegalStateException("Unknown clause: " + cNode.getTag());
}
}
ILogicalExpression rExpr = vre(translateExpression(fNode.getReturnExpr(), tCtx));
List<LogicalVariable> vars = new ArrayList<LogicalVariable>();
List<Mutable<ILogicalExpression>> exprs = new ArrayList<Mutable<ILogicalExpression>>();
LogicalVariable var = newLogicalVariable();
vars.add(var);
exprs.add(mutable(afce(BuiltinOperators.SEQUENCE, false, rExpr)));
AggregateOperator aop = new AggregateOperator(vars, exprs);
aop.getInputs().add(mutable(tCtx.op));
tCtx.op = aop;