Debian Codegraph @

net/sf/saxon/expr/flwor/OrderByClause.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.*;
11 import net.sf.saxon.expr.parser.*;
12 import net.sf.saxon.expr.sort.AtomicComparer;
13 import net.sf.saxon.expr.sort.SortKeyDefinition;
14 import net.sf.saxon.expr.sort.SortKeyDefinitionList;
15 import net.sf.saxon.trace.ExpressionPresenter;
16 import net.sf.saxon.trans.XPathException;
17 import net.sf.saxon.tree.util.FastStringBuffer;
18 import net.sf.saxon.value.AtomicValue;
19 import net.sf.saxon.value.SequenceType;
20
21 /**
22 * This class represents an "order by" clause in a FLWOR expression
23 */
24 public class OrderByClause extends Clause {
25
26 Operand sortKeysOp; // Holds a SortKeyDefinitionList
27 AtomicComparer[] comparators;
28 Operand tupleOp; // Holds a TupleExpression
29
30 public OrderByClause(FLWORExpression flwor, SortKeyDefinition[] sortKeys, TupleExpression tupleExpression) {
31 this.sortKeysOp = new Operand(flwor, new SortKeyDefinitionList(sortKeys), OperandRole.REPEAT_NAVIGATE_CONSTRAINED);
32 this.tupleOp = new Operand(flwor, tupleExpression, OperandRole.REPEAT_NAVIGATE_CONSTRAINED);
33 }
34
35 @Override
36 public int getClauseKey() {
37 return ORDERBYCLAUSE;
38 }
39
40 @Override
41 public boolean containsNonInlineableVariableReference(Binding binding) {
42 return getTupleExpression().includesBinding(binding);
43 }
44
45 public OrderByClause copy(FLWORExpression flwor, RebindingMap rebindings) {
46 SortKeyDefinitionList sortKeys = getSortKeyDefinitions();
47 SortKeyDefinition[] sk2 = new SortKeyDefinition[sortKeys.size()];
48 for (int i = 0; i < sortKeys.size(); i++) {
49 sk2[i] = sortKeys.getSortKeyDefinition(i).copy(rebindings);
50 }
51 OrderByClause obc = new OrderByClause(flwor, sk2, (TupleExpression) getTupleExpression().copy(rebindings));
52 obc.setLocation(getLocation());
53 obc.setPackageData(getPackageData());
54 obc.comparators = comparators;
55 return obc;
56 }
57
58 public SortKeyDefinitionList getSortKeyDefinitions() {
59 return (SortKeyDefinitionList)sortKeysOp.getChildExpression();
60 }
61
62 public AtomicComparer[] getAtomicComparers() {
63 return comparators;
64 }
65
66 public TupleExpression getTupleExpression() {
67 return (TupleExpression)tupleOp.getChildExpression();
68 }
69
70 /**
71 * Get a tuple stream that implements the functionality of this clause, taking its
72 * input from another tuple stream which this clause modifies
73 *
74 * @param base the input tuple stream
75 * @param context XQuery dynamic context
76 * @return the output tuple stream
77 */
78 @Override
79 public TuplePull getPullStream(TuplePull base, XPathContext context) {
80 return new OrderByClausePull(base, getTupleExpression(), this, context);
81 }
82
83 /**
84 * Get a push-mode tuple stream that implements the functionality of this clause, supplying its
85 * output to another tuple stream
86 *
87 * @param destination the output tuple stream
88 * @param context XQuery dynamic context
89 * @return the push tuple stream that implements the functionality of this clause of the FLWOR
90 * expression
91 */
92 @Override
93 public TuplePush getPushStream(TuplePush destination, XPathContext context) {
94 return new OrderByClausePush(destination, getTupleExpression(), this, context);
95 }
96
97 /**
98 * Process the subexpressions of this clause
99 *
100 * @param processor the expression processor used to process the subexpressions
101 */
102 @Override
103 public void processOperands(OperandProcessor processor) throws XPathException {
104 processor.processOperand(tupleOp);
105 processor.processOperand(sortKeysOp);
106 // for (SortKeyDefinition sortKey : sortKeys) {
107 // sortKey.processSubExpressions(processor);
108 // }
109 }
110
111 /**
112 * Type-check the expression
113 */
114
115 public void typeCheck(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException {
116 boolean allKeysFixed = true;
117 SortKeyDefinitionList sortKeys = getSortKeyDefinitions();
118 for (SortKeyDefinition sk : sortKeys) {
119 if (!sk.isFixed()) {
120 allKeysFixed = false;
121 break;
122 }
123 }
124
125 if (allKeysFixed) {
126 comparators = new AtomicComparer[sortKeys.size()];
127 }
128
129 int i=0;
130 TypeChecker tc = visitor.getConfiguration().getTypeChecker(false);
131 for (SortKeyDefinition skd : sortKeys) {
132 Expression sortKey = skd.getSortKey();
133 RoleDiagnostic role = new RoleDiagnostic(RoleDiagnostic.ORDER_BY, "", i);
134 role.setErrorCode("XPTY0004");
135 sortKey = tc.staticTypeCheck(sortKey, SequenceType.OPTIONAL_ATOMIC, role, visitor);
136 skd.setSortKey(sortKey, false);
137 skd.typeCheck(visitor, contextInfo);
138 if (skd.isFixed()) {
139 AtomicComparer comp = skd.makeComparator(
140 visitor.getStaticContext().makeEarlyEvaluationContext());
141 skd.setFinalComparator(comp);
142 if (allKeysFixed) {
143 comparators[i] = comp;
144 }
145 }
146 i++;
147 }
148 }
149
150 public void addToPathMap(PathMap pathMap, PathMap.PathMapNodeSet pathMapNodeSet) {
151 SortKeyDefinitionList sortKeys = getSortKeyDefinitions();
152 for (SortKeyDefinition skd : sortKeys) {
153 Expression sortKey = skd.getSortKey();
154 sortKey.addToPathMap(pathMap, pathMapNodeSet);
155
156 }
157 }
158
159 /**
160 * Diagnostic print of expression structure. The abstract expression tree
161 * is written to the supplied output destination.
162 *
163 * @param out the expression presenter used to display the structure
164 */
165 @Override
166 public void explain(ExpressionPresenter out) throws XPathException {
167 out.startElement("order-by");
168 for (SortKeyDefinition k : getSortKeyDefinitions()) {
169 out.startSubsidiaryElement("key");
170 k.getSortKey().export(out);
171 out.endSubsidiaryElement();
172 }
173 out.endElement();
174 }
175
176 public String toString() {
177 FastStringBuffer fsb = new FastStringBuffer(FastStringBuffer.C64);
178 fsb.append("order by ... ");
179 return fsb.toString();
180 }
181
182 /**
183 * Callback for evaluating the sort keys
184 *
185 * @param n identifies the sort key to be evaluated
186 * @param c the dynamic context for evaluation of the sort key
187 * @return the value of the sort key
188 * @throws XPathException if evaluation of the sort key fails
189 */
190
191 /*@Nullable*/
192 public AtomicValue evaluateSortKey(int n, XPathContext c) throws XPathException {
193 SortKeyDefinitionList sortKeys = getSortKeyDefinitions();
194 return (AtomicValue) sortKeys.getSortKeyDefinition(n).getSortKey().evaluateItem(c);
195 }
196
197 }
198