Debian Codegraph @

net/sf/saxon/expr/flwor/GroupByClausePush.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
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 * Represents the tuple stream delivered by an "group by" clause. This groups the tuple stream supplied
25 * as its input, and outputs a new set of tuples one per group of the input tuples. No groups are output
26 * until all the groups have been read.
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 * Move on to the next tuple. Before returning, this method must set all the variables corresponding
48 * to the "returned" tuple in the local stack frame associated with the context object
49 *
50 * @param context the dynamic evaluation context
51 */
52 @Override
53 public void processTuple(XPathContext context) throws XPathException {
54
55 // Allocate the incoming tuple to a group
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 * Close the tuple stream, indicating that although not all tuples have been read,
102 * no further tuples are required and resources can be released
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 // Copyright (c) 2011 Saxonica Limited.