| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.expr.flwor; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.XPathContext; |
| 11 |
import net.sf.saxon.expr.sort.GenericAtomicComparer; |
| 12 |
import net.sf.saxon.om.Sequence; |
| 13 |
import net.sf.saxon.trans.XPathException; |
| 14 |
|
| 15 |
import java.util.ArrayList; |
| 16 |
import java.util.HashMap; |
| 17 |
import java.util.Iterator; |
| 18 |
import java.util.List; |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
public class GroupByClausePull extends TuplePull { |
| 26 |
|
| 27 |
private TuplePull base; |
| 28 |
private GroupByClause groupByClause; |
| 29 |
Iterator<List<GroupByClause.ObjectToBeGrouped>> groupIterator; |
| 30 |
private GenericAtomicComparer[] comparers; |
| 31 |
|
| 32 |
|
| 33 |
public GroupByClausePull(TuplePull base, GroupByClause groupBy, XPathContext context) { |
| 34 |
this.base = base; |
| 35 |
this.groupByClause = groupBy; |
| 36 |
comparers = new GenericAtomicComparer[groupBy.comparers.length]; |
| 37 |
for (int i = 0; i < comparers.length; i++) { |
| 38 |
comparers[i] = groupBy.comparers[i].provideContext(context); |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
@Override |
| 52 |
public boolean nextTuple(XPathContext context) throws XPathException { |
| 53 |
if (groupIterator == null) { |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
TupleExpression groupingTupleExpr = groupByClause.getGroupingTupleExpression(); |
| 58 |
TupleExpression retainedTupleExpr = groupByClause.getRetainedTupleExpression(); |
| 59 |
HashMap<Object, List<GroupByClause.ObjectToBeGrouped>> map = new HashMap<>(); |
| 60 |
while (base.nextTuple(context)) { |
| 61 |
GroupByClause.ObjectToBeGrouped otbg = new GroupByClause.ObjectToBeGrouped(); |
| 62 |
Sequence[] groupingValues = groupingTupleExpr.evaluateItem(context).getMembers(); |
| 63 |
GroupByClausePush.checkGroupingValues(groupingValues); |
| 64 |
otbg.groupingValues = new Tuple(groupingValues); |
| 65 |
otbg.retainedValues = retainedTupleExpr.evaluateItem(context); |
| 66 |
Object key = groupByClause.getComparisonKey(otbg.groupingValues, comparers); |
| 67 |
List<GroupByClause.ObjectToBeGrouped> group = map.get(key); |
| 68 |
GroupByClausePush.addToGroup(key, otbg, group, map); |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
groupIterator = map.values().iterator(); |
| 73 |
|
| 74 |
} |
| 75 |
|
| 76 |
if (groupIterator.hasNext()) { |
| 77 |
List<GroupByClause.ObjectToBeGrouped> group = groupIterator.next(); |
| 78 |
groupByClause.processGroup(group, context); |
| 79 |
return true; |
| 80 |
} else { |
| 81 |
return false; |
| 82 |
} |
| 83 |
|
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
@Override |
| 93 |
public void close() { |
| 94 |
base.close(); |
| 95 |
groupIterator = null; |
| 96 |
} |
| 97 |
|
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|