| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.expr.flwor; |
| 9 |
|
| 10 |
import net.sf.saxon.Configuration; |
| 11 |
import net.sf.saxon.expr.*; |
| 12 |
import net.sf.saxon.expr.parser.ContextItemStaticInfo; |
| 13 |
import net.sf.saxon.expr.parser.ExpressionVisitor; |
| 14 |
import net.sf.saxon.expr.parser.PathMap; |
| 15 |
import net.sf.saxon.expr.parser.RebindingMap; |
| 16 |
import net.sf.saxon.expr.sort.GenericAtomicComparer; |
| 17 |
import net.sf.saxon.functions.DeepEqual; |
| 18 |
import net.sf.saxon.om.Item; |
| 19 |
import net.sf.saxon.om.Sequence; |
| 20 |
import net.sf.saxon.om.SequenceIterator; |
| 21 |
import net.sf.saxon.trace.ExpressionPresenter; |
| 22 |
import net.sf.saxon.trans.XPathException; |
| 23 |
import net.sf.saxon.tree.util.FastStringBuffer; |
| 24 |
import net.sf.saxon.value.AtomicValue; |
| 25 |
import net.sf.saxon.value.SequenceExtent; |
| 26 |
|
| 27 |
import java.util.ArrayList; |
| 28 |
import java.util.Arrays; |
| 29 |
import java.util.LinkedList; |
| 30 |
import java.util.List; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public class GroupByClause extends Clause { |
| 36 |
|
| 37 |
Configuration config; |
| 38 |
LocalVariableBinding[] bindings; |
| 39 |
|
| 40 |
GenericAtomicComparer[] comparers; |
| 41 |
|
| 42 |
Operand retainedTupleOp; |
| 43 |
Operand groupingTupleOp; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public GroupByClause(Configuration config) { |
| 54 |
this.config = config; |
| 55 |
} |
| 56 |
|
| 57 |
@Override |
| 58 |
public int getClauseKey() { |
| 59 |
return GROUPBYCLAUSE; |
| 60 |
} |
| 61 |
|
| 62 |
@Override |
| 63 |
public boolean containsNonInlineableVariableReference(Binding binding) { |
| 64 |
return getRetainedTupleExpression().includesBinding(binding) || |
| 65 |
getGroupingTupleExpression().includesBinding(binding); |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
public GroupByClause copy(FLWORExpression flwor, RebindingMap rebindings) { |
| 70 |
GroupByClause g2 = new GroupByClause(config); |
| 71 |
g2.setLocation(getLocation()); |
| 72 |
g2.setPackageData(getPackageData()); |
| 73 |
g2.bindings = new LocalVariableBinding[bindings.length]; |
| 74 |
for (int i = 0; i < bindings.length; i++) { |
| 75 |
g2.bindings[i] = bindings[i].copy(); |
| 76 |
} |
| 77 |
g2.comparers = comparers; |
| 78 |
g2.initRetainedTupleExpression(flwor, (TupleExpression)getRetainedTupleExpression().copy(rebindings)); |
| 79 |
g2.initGroupingTupleExpression(flwor, (TupleExpression)getGroupingTupleExpression().copy(rebindings)); |
| 80 |
return g2; |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
public void initRetainedTupleExpression(FLWORExpression flwor, TupleExpression expr) { |
| 91 |
retainedTupleOp = new Operand(flwor, expr, OperandRole.REPEAT_NAVIGATE_CONSTRAINED); |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
public void setRetainedTupleExpression(TupleExpression expr) { |
| 102 |
retainedTupleOp.setChildExpression(expr); |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
public TupleExpression getRetainedTupleExpression() { |
| 113 |
return (TupleExpression)retainedTupleOp.getChildExpression(); |
| 114 |
} |
| 115 |
|
| 116 |
@Override |
| 117 |
public void optimize(ExpressionVisitor visitor, ContextItemStaticInfo contextItemType) throws XPathException { |
| 118 |
LinkedList<LocalVariableBinding> list = new LinkedList<>(Arrays.asList(bindings)); |
| 119 |
LinkedList<LocalVariableReference> retainingExpr = new LinkedList<>(); |
| 120 |
for (Operand o : getRetainedTupleExpression().operands()) { |
| 121 |
retainingExpr.add((LocalVariableReference)o.getChildExpression()); |
| 122 |
} |
| 123 |
|
| 124 |
int groupingSize = getGroupingTupleExpression().getSize(); |
| 125 |
for (int i = groupingSize; i < list.size(); i++) { |
| 126 |
if (list.get(i).getNominalReferenceCount() == 0) { |
| 127 |
list.remove(i); |
| 128 |
retainingExpr.remove(i - groupingSize); |
| 129 |
} |
| 130 |
} |
| 131 |
bindings = list.toArray(new LocalVariableBinding[0]); |
| 132 |
getRetainedTupleExpression().setVariables(retainingExpr); |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
public void initGroupingTupleExpression(FLWORExpression flwor, TupleExpression expr) { |
| 143 |
groupingTupleOp = new Operand(flwor, expr, OperandRole.REPEAT_NAVIGATE_CONSTRAINED); |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
public void setGroupingTupleExpression(TupleExpression expr) { |
| 154 |
groupingTupleOp.setChildExpression(expr); |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
public TupleExpression getGroupingTupleExpression() { |
| 165 |
return (TupleExpression)groupingTupleOp.getChildExpression(); |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
public void setVariableBindings(LocalVariableBinding[] bindings) { |
| 178 |
this.bindings = bindings; |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
public LocalVariableBinding[] getRangeVariables() { |
| 188 |
return bindings; |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
public void setComparers(GenericAtomicComparer[] comparers) { |
| 198 |
this.comparers = comparers; |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
@Override |
| 210 |
public TuplePull getPullStream(TuplePull base, XPathContext context) { |
| 211 |
return new GroupByClausePull(base, this, context); |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
@Override |
| 224 |
public TuplePush getPushStream(TuplePush destination, XPathContext context) { |
| 225 |
return new GroupByClausePush(destination, this, context); |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
@Override |
| 234 |
public void processOperands(OperandProcessor processor) throws XPathException { |
| 235 |
processor.processOperand(groupingTupleOp); |
| 236 |
processor.processOperand(retainedTupleOp); |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
@Override |
| 246 |
public void explain(ExpressionPresenter out) { |
| 247 |
out.startElement("group-by"); |
| 248 |
for (Operand o : getRetainedTupleExpression().operands()) { |
| 249 |
LocalVariableReference ref = (LocalVariableReference)o.getChildExpression(); |
| 250 |
out.startSubsidiaryElement("by"); |
| 251 |
out.emitAttribute("var", ref.getDisplayName()); |
| 252 |
out.emitAttribute("slot", ref.getBinding().getLocalSlotNumber() + ""); |
| 253 |
out.endSubsidiaryElement(); |
| 254 |
} |
| 255 |
out.endElement(); |
| 256 |
} |
| 257 |
|
| 258 |
public String toString() { |
| 259 |
FastStringBuffer fsb = new FastStringBuffer(FastStringBuffer.C64); |
| 260 |
fsb.append("group by ... "); |
| 261 |
return fsb.toString(); |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
public static class ObjectToBeGrouped { |
| 271 |
public Tuple groupingValues; |
| 272 |
public Tuple retainedValues; |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
public void processGroup(List<ObjectToBeGrouped> group, XPathContext context) throws XPathException { |
| 287 |
LocalVariableBinding[] bindings = getRangeVariables(); |
| 288 |
Sequence<?>[] groupingValues = group.get(0).groupingValues.getMembers(); |
| 289 |
for (int j = 0; j < groupingValues.length; j++) { |
| 290 |
Sequence<?> v = groupingValues[j]; |
| 291 |
context.setLocalVariable(bindings[j].getLocalSlotNumber(), v); |
| 292 |
} |
| 293 |
for (int j = groupingValues.length; j < bindings.length; j++) { |
| 294 |
List<Item<?>> concatenatedValue = new ArrayList<>(); |
| 295 |
for (ObjectToBeGrouped otbg : group) { |
| 296 |
Sequence<?> val = otbg.retainedValues.getMembers()[j - groupingValues.length]; |
| 297 |
SequenceIterator<?> si = val.iterate(); |
| 298 |
Item<?> it; |
| 299 |
while ((it = si.next()) != null) { |
| 300 |
concatenatedValue.add(it); |
| 301 |
} |
| 302 |
} |
| 303 |
SequenceExtent<Item<?>> se = new SequenceExtent<>(concatenatedValue); |
| 304 |
context.setLocalVariable(bindings[j].getLocalSlotNumber(), se); |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
public TupleComparisonKey getComparisonKey(Tuple t, GenericAtomicComparer[] comparers) { |
| 317 |
return new TupleComparisonKey(t.getMembers(), comparers); |
| 318 |
} |
| 319 |
|
| 320 |
@Override |
| 321 |
public void addToPathMap(PathMap pathMap, PathMap.PathMapNodeSet pathMapNodeSet) { |
| 322 |
throw new UnsupportedOperationException("Cannot use document projection with group-by"); |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
public class TupleComparisonKey { |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
private Sequence[] groupingValues; |
| 336 |
private GenericAtomicComparer[] comparers; |
| 337 |
|
| 338 |
public TupleComparisonKey(Sequence[] groupingValues, GenericAtomicComparer[] comparers) { |
| 339 |
this.groupingValues = groupingValues; |
| 340 |
this.comparers = comparers; |
| 341 |
} |
| 342 |
|
| 343 |
public int hashCode() { |
| 344 |
int h = 0x77557755 ^ groupingValues.length; |
| 345 |
for (int i = 0; i < groupingValues.length; i++) { |
| 346 |
GenericAtomicComparer comparer = comparers[i]; |
| 347 |
int implicitTimezone = comparer.getContext().getImplicitTimezone(); |
| 348 |
try { |
| 349 |
SequenceIterator atoms = groupingValues[i].iterate(); |
| 350 |
while (true) { |
| 351 |
AtomicValue val = (AtomicValue) atoms.next(); |
| 352 |
if (val == null) { |
| 353 |
break; |
| 354 |
} |
| 355 |
h ^= i + val.getXPathComparable(false, comparer.getCollator(), implicitTimezone).hashCode(); |
| 356 |
} |
| 357 |
} catch (XPathException e) { |
| 358 |
|
| 359 |
} |
| 360 |
} |
| 361 |
return h; |
| 362 |
} |
| 363 |
|
| 364 |
public boolean equals(Object other) { |
| 365 |
if (!(other instanceof TupleComparisonKey)) { |
| 366 |
return false; |
| 367 |
} |
| 368 |
if (groupingValues.length != ((TupleComparisonKey) other).groupingValues.length) { |
| 369 |
return false; |
| 370 |
} |
| 371 |
for (int i = 0; i < groupingValues.length; i++) { |
| 372 |
try { |
| 373 |
if (!DeepEqual.deepEqual( |
| 374 |
groupingValues[i].iterate(), |
| 375 |
((TupleComparisonKey) other).groupingValues[i].iterate(), |
| 376 |
comparers[i], comparers[i].getContext(), 0)) { |
| 377 |
return false; |
| 378 |
} |
| 379 |
} catch (XPathException e) { |
| 380 |
return false; |
| 381 |
} |
| 382 |
} |
| 383 |
return true; |
| 384 |
} |
| 385 |
} |
| 386 |
|
| 387 |
} |
| 388 |
|
| 389 |
|