Debian Codegraph @

net/sf/saxon/expr/flwor/GroupByClausePull.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.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 * Represents the tuple stream delivered by an "group by" clause. This groups the tuple stream supplied
22 * as its input, and outputs a new set of tuples one per group of the input tuples. No groups are output
23 * until all the groups have been read.
24 */
25 public class GroupByClausePull extends TuplePull {
26
27 private TuplePull base;
28 private GroupByClause groupByClause;
29 /*@Nullable*/ 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 * Move on to the next tuple. Before returning, this method must set all the variables corresponding
44 * to the "returned" tuple in the local stack frame associated with the context object
45 *
46 * @param context the dynamic evaluation context
47 * @return true if another tuple has been generated; false if the tuple stream is exhausted. If the
48 * method returns false, the values of the local variables corresponding to this tuple stream
49 * are undefined.
50 */
51 @Override
52 public boolean nextTuple(XPathContext context) throws XPathException {
53 if (groupIterator == null) {
54
55 // First do the grouping
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 // get an iterator over the groups
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 * Close the tuple stream, indicating that although not all tuples have been read,
89 * no further tuples are required and resources can be released
90 */
91
92 @Override
93 public void close() {
94 base.close();
95 groupIterator = null;
96 }
97
98 }
99
100 // Copyright (c) 2011 Saxonica Limited.
101
102