| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.expr.flwor; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.Atomizer; |
| 11 |
import net.sf.saxon.expr.XPathContext; |
| 12 |
import net.sf.saxon.expr.sort.GenericAtomicComparer; |
| 13 |
import net.sf.saxon.om.Sequence; |
| 14 |
import net.sf.saxon.om.SequenceTool; |
| 15 |
import net.sf.saxon.trans.XPathException; |
| 16 |
import net.sf.saxon.value.AtomicValue; |
| 17 |
import net.sf.saxon.value.EmptySequence; |
| 18 |
|
| 19 |
import java.util.ArrayList; |
| 20 |
import java.util.HashMap; |
| 21 |
import java.util.List; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
public class GroupByClausePush extends TuplePush { |
| 29 |
|
| 30 |
private TuplePush destination; |
| 31 |
private GroupByClause groupByClause; |
| 32 |
private HashMap<Object, List<GroupByClause.ObjectToBeGrouped>> map = new HashMap<>(); |
| 33 |
private XPathContext context; |
| 34 |
private GenericAtomicComparer[] comparers; |
| 35 |
|
| 36 |
public GroupByClausePush(TuplePush destination, GroupByClause groupBy, XPathContext context) { |
| 37 |
this.destination = destination; |
| 38 |
this.groupByClause = groupBy; |
| 39 |
this.context = context; |
| 40 |
comparers = new GenericAtomicComparer[groupBy.comparers.length]; |
| 41 |
for (int i=0; i<comparers.length; i++) { |
| 42 |
comparers[i] = groupBy.comparers[i].provideContext(context); |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
@Override |
| 53 |
public void processTuple(XPathContext context) throws XPathException { |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
TupleExpression groupingTupleExpr = groupByClause.getGroupingTupleExpression(); |
| 58 |
TupleExpression retainedTupleExpr = groupByClause.getRetainedTupleExpression(); |
| 59 |
|
| 60 |
|
| 61 |
GroupByClause.ObjectToBeGrouped otbg = new GroupByClause.ObjectToBeGrouped(); |
| 62 |
Sequence[] groupingValues = groupingTupleExpr.evaluateItem(context).getMembers(); |
| 63 |
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 |
addToGroup(key, otbg, group, map); |
| 69 |
} |
| 70 |
|
| 71 |
protected static void addToGroup( |
| 72 |
Object key, |
| 73 |
GroupByClause.ObjectToBeGrouped objectToBeGrouped, |
| 74 |
List<GroupByClause.ObjectToBeGrouped> group, |
| 75 |
HashMap<Object, List<GroupByClause.ObjectToBeGrouped>> map) { |
| 76 |
if (group != null) { |
| 77 |
group.add(objectToBeGrouped); |
| 78 |
map.put(key, group); |
| 79 |
} else { |
| 80 |
List<GroupByClause.ObjectToBeGrouped> list = new ArrayList<>(); |
| 81 |
list.add(objectToBeGrouped); |
| 82 |
map.put(key, list); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
protected static void checkGroupingValues(Sequence[] groupingValues) throws XPathException { |
| 87 |
for (int i = 0; i < groupingValues.length; i++) { |
| 88 |
Sequence<?> v = groupingValues[i]; |
| 89 |
if (!(v instanceof EmptySequence || v instanceof AtomicValue)) { |
| 90 |
v = Atomizer.getAtomizingIterator(v.iterate(), false).materialize(); |
| 91 |
if (SequenceTool.getLength(v) > 1) { |
| 92 |
throw new XPathException("Grouping key value cannot be a sequence of more than one item", "XPTY0004"); |
| 93 |
} |
| 94 |
groupingValues[i] = v; |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
@Override |
| 106 |
public void close() throws XPathException { |
| 107 |
|
| 108 |
for (List<GroupByClause.ObjectToBeGrouped> group : map.values()) { |
| 109 |
groupByClause.processGroup(group, context); |
| 110 |
destination.processTuple(context); |
| 111 |
} |
| 112 |
|
| 113 |
destination.close(); |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
} |
| 118 |
|
| 119 |
|