}
}
// Check if we can speed up the processing of the "order by" clause.
boolean fastOrderBy = false;
LocalVariable var;
Sequence in;
// Save the local variable stack
LocalVariable mark = context.markLocalVariables(false);
try {
// Evaluate the "in" expression
in = inputSequence.eval(contextSequence, null);
clearContext(getExpressionId(), in);
// Declare the iteration variable
var = new LocalVariable(QName.parse(context, varName, null));
var.setSequenceType(sequenceType);
context.declareVariableBinding(var);
registerUpdateListener(in);
// Declare positional variable
LocalVariable at = null;
if (positionalVariable != null) {
at = new LocalVariable(QName.parse(context, positionalVariable, null));
at.setSequenceType(POSITIONAL_VAR_TYPE);
context.declareVariableBinding(at);
}
// Assign the whole input sequence to the bound variable.
// This is required if we process the "where" or "order by" clause
// in one step.
var.setValue(in);
// Save the current context document set to the variable as a hint
// for path expressions occurring in the "return" clause.
if (in instanceof NodeSet) {
var.setContextDocs(in.getDocumentSet());
} else {
var.setContextDocs(null);
}
// See if we can process the "where" clause in a single step (instead of
// calling the where expression for each item in the input sequence)
// This is possible if the input sequence is a node set and has no
// dependencies on the current context item.
boolean fastExec =
whereExpr != null && at == null &&
!Dependency.dependsOn(whereExpr, Dependency.CONTEXT_ITEM) &&
in.isPersistentSet() &&
Type.subTypeOf(in.getItemType(), Type.NODE);
// If possible, apply the where expression ahead of the iteration
if (fastExec) {
if (!in.isCached()) {
setContext(getExpressionId(), in);
if (whereExpr != null)
{whereExpr.setContextId(getExpressionId());}
}
in = applyWhereExpression(in);
if (!in.isCached())
{clearContext(getExpressionId(), in);}
}
// PreorderedValueSequence applies the order specs to all items
// in one single processing step
if (fastOrderBy) {
in = new PreorderedValueSequence(orderSpecs, in, getExpressionId());
}
// Otherwise, if there's an order by clause, wrap the result into
// an OrderedValueSequence. OrderedValueSequence will compute
// order expressions for every item when it is added to the result sequence.
if (resultSequence == null) {
if (orderSpecs != null && !fastOrderBy) {
resultSequence = new OrderedValueSequence(orderSpecs, in.getItemCount());
} else {
resultSequence = new ValueSequence();
((ValueSequence)resultSequence).keepUnOrdered(unordered);
}
}
Sequence val = null;
int p = 1;
final IntegerValue atVal = new IntegerValue(1);
if(positionalVariable != null)
{at.setValue(atVal);}
//Type.EMPTY is *not* a subtype of other types ;
//the tests below would fail without this prior cardinality check
if (in.isEmpty() && sequenceType != null &&
!Cardinality.checkCardinality(sequenceType.getCardinality(),
Cardinality.EMPTY)) {
throw new XPathException(this, ErrorCodes.XPTY0004,
"Invalid cardinality for variable $" + varName +
". Expected " + Cardinality.getDescription(sequenceType.getCardinality()) +
", got " + Cardinality.getDescription(in.getCardinality()));
}
// Loop through each variable binding
p = 0;
for (final SequenceIterator i = in.iterate(); i.hasNext(); p++) {
context.proceed(this);
contextItem = i.nextItem();
context.setContextSequencePosition(p, in);
if (positionalVariable != null)
{at.setValue(new IntegerValue(p + 1));}
contextSequence = contextItem.toSequence();
// set variable value to current item
var.setValue(contextSequence);
if (sequenceType == null)
{var.checkType();} //because it makes some conversions !
val = contextSequence;
// check optional where clause
if (whereExpr != null && (!fastExec)) {
if (contextItem instanceof NodeProxy)
{((NodeProxy)contextItem).addContextNode(getExpressionId(), (NodeProxy)contextItem);}
final Sequence bool = applyWhereExpression(null);
if (contextItem instanceof NodeProxy)
{((NodeProxy)contextItem).clearContext(getExpressionId());}
// if where returned false, continue
if (!bool.effectiveBooleanValue())
{continue;}
} else {
val = contextItem.toSequence();
}
//Reset the context position
context.setContextSequencePosition(0, null);
if (groupedSequence==null) {
if (returnExpr instanceof BindingExpression) {
((BindingExpression)returnExpr).eval(null, null, resultSequence, null);
// otherwise call the return expression and add results to resultSequence
} else {
val = returnExpr.eval(null);
resultSequence.addAll(val);
}
} else {
/* bv : special processing for groupby :
if returnExpr is a Binding expression, pass the groupedSequence.
Else, add item to groupedSequence and don't evaluate here !
*/
if (returnExpr instanceof BindingExpression){
((BindingExpression)returnExpr).eval(null, null, resultSequence, groupedSequence);
} else {
final Sequence toGroupSequence = context.resolveVariable(groupedSequence.getToGroupVarName()).getValue();
groupedSequence.addAll(toGroupSequence);
}
}
// free resources
var.destroy(context, resultSequence);
}
} finally {
// restore the local variable stack
context.popLocalVariables(mark, resultSequence);
}
// bv : Special processing for groupBy : one return per group in groupedSequence
if (groupSpecs!=null) {
mark = context.markLocalVariables(false);
context.declareVariableBinding(var);
// Declare positional variable if required
LocalVariable at = null;
if (positionalVariable != null) {
at = new LocalVariable(QName.parse(context, positionalVariable, null));
at.setSequenceType(POSITIONAL_VAR_TYPE);
context.declareVariableBinding(at);
}
final IntegerValue atVal = new IntegerValue(1);
if(positionalVariable != null) {
at.setValue(atVal);
}
int p = 0;
for (final Iterator<String> it = groupedSequence.iterate(); it.hasNext(); ) {
final GroupedValueSequence currentGroup = groupedSequence.get(it.next());
context.proceed(this);
// set binding variable to current group
var.setValue(currentGroup);
var.checkType();
//set value of grouping keys for the current group
for (int i=0; i< groupKeyVar.length ; i ++) {
groupKeyVar[i].setValue(currentGroup.getGroupKey().itemAt(i).toSequence());
}
if (positionalVariable != null) {
final ValueSequence ps = new ValueSequence();
for (int i = 0; i < currentGroup.getItemCount(); i++) {
ps.add(new IntegerValue(p + i + 1));
}
at.setValue(ps);
}
//evaluate real return expression
final Sequence val = groupReturnExpr.eval(null);
resultSequence.addAll(val);
p += currentGroup.getItemCount();
}
//Reset the context position