Skip to content

Commit

Permalink
[bugfix] Quick fix to support for positional variable in group by.
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfgangmm committed May 24, 2014
1 parent 7b4b607 commit a3f76c6
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions src/org/exist/xquery/ForExpr.java
Expand Up @@ -326,12 +326,25 @@ public Sequence eval(Sequence contextSequence, Item contextItem,
// restore the local variable stack
context.popLocalVariables(mark, resultSequence);
}
// bv : Special processing for groupBy : one return per group in groupedSequence
//TODO : positional variable !
if (groupSpecs!=null){
// bv : Special processing for groupBy : one return per group in groupedSequence
if (groupSpecs!=null) {
mark = context.markLocalVariables(false);
context.declareVariableBinding(var);
for (final Iterator<String> it = groupedSequence.iterate(); it.hasNext(); ){

// 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
Expand All @@ -341,9 +354,18 @@ public Sequence eval(Sequence contextSequence, Item contextItem,
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
context.setContextSequencePosition(0, null);
Expand Down

0 comments on commit a3f76c6

Please sign in to comment.