1 22 23 package org.xquark.mediator.plan; 24 25 import java.util.ArrayList ; 26 27 import org.xquark.mediator.DOMUtils.BufferTuple; 28 import org.xquark.mediator.DOMUtils.EvaluationVisitor; 29 import org.xquark.mediator.DOMUtils.Tuple; 30 import org.xquark.mediator.runtime.MediatorException; 31 import org.xquark.xquery.parser.*; 32 import org.xquark.xquery.parser.primitivefunctions.fnfunctions.FunctionCOUNT; 33 34 public class OpAggregateFunc extends OpUn { 35 private static final String RCSRevision = "$Revision: 1.9 $"; 39 private static final String RCSName = "$Name: $"; 44 public OpAggregateFunc(ExecutionPlan plan, XQueryExpression expression, Operator childexp) throws MediatorException { 45 super(plan, expression, childexp); 46 if (paths != null) 48 paths.clear(); 49 if (paths == null) 51 paths = new ArrayList (); 52 getRetPath(((FunctionCall) expression).getArgument(0), paths); 53 if (paths.isEmpty() || paths.size() > 1) 54 throw new MediatorException("Could not find paths for aggregate function call"); 55 ((FunctionCall) expression).getArguments().set(0, paths.get(0)); 57 size = 1; 58 idsize = 0; 59 } 60 61 private void getRetPath(XQueryExpression expr, ArrayList reslist) { 63 if (expr instanceof XQueryExpressionSequence) { 64 for (int i = 0; i < ((XQueryExpressionSequence) expr).getSubExpressions().size(); i++) { 65 getRetPath((XQueryExpression) ((XQueryExpressionSequence) expr).getSubExpressions().get(i), reslist); 66 } 67 } 68 if (expr instanceof LocatedExpression || expr instanceof Value || expr instanceof Variable) { 69 reslist.add(expr); 70 } else if (expr instanceof FLWRExpression) { 71 getRetPath(((FLWRExpression) expr).getReturnClause(), reslist); 72 } else if (expr instanceof FunctionCall) { 73 getRetPath(((FunctionCall) expr).getArgument(0), reslist); 74 } 75 } 76 77 81 public void accept(OperatorVisitor visitor) throws MediatorException { 82 visitor.visit(this); 83 } 84 85 91 public ResultSet getResultSet(DynamicContext context, OperatorRunnable child) throws MediatorException { 92 return new AggregateFuncResultSet(this, context, child); 93 } 94 95 104 106 112 } 113 114 class AggregateFuncResultSet extends UnResultSet { 115 private static final String RCSRevision = "$Revision: 1.9 $"; 119 private static final String RCSName = "$Name: $"; 122 public AggregateFuncResultSet(OpAggregateFunc algebra, DynamicContext context, OperatorRunnable child) throws MediatorException { 126 super(algebra, context, child); 127 } 128 129 140 protected void evaluate(boolean non_blocking) throws MediatorException { 141 145 if (!(this.getOperator().getExpression() instanceof FunctionCOUNT) && !resultset.hasNext()) { 147 resultset.close(); 149 setFinishedWhenEmpty(); 150 return; 151 } 152 153 BufferTuple tmpBufferTuple = new BufferTuple(resultset.getOperator().getPaths(), 0); 154 if (resultset.hasNext()) { 155 while (resultset.hasNext()) { 156 Tuple tuple = resultset.next(); 157 tuple.eraseIdSize(); 158 tmpBufferTuple.add(tuple); 159 } 160 } 161 162 resultset.close(); 163 try { 164 EvaluationVisitor visitor = new EvaluationVisitor(this.operator.getPlan().getSchemaManager()); 165 visitor.reset(tmpBufferTuple); 166 visitor.setReturnType(EvaluationVisitor.TUPLE_TYPE); 167 this.getOperator().getExpression().accept(visitor); 168 Tuple tuple = visitor.getResultTuple(); 169 if (tuple != null) 170 this.buftuples.add(tuple); 171 } catch (XQueryException xqe) { 172 throw new MediatorException(xqe.getMessage(), xqe); 173 } 174 175 setFinishedWhenEmpty(); 176 } 177 } 178 | Popular Tags |