afs commented on a change in pull request #568: Add Aggregate Median to SPARQL
ARQ syntax
URL: https://github.com/apache/jena/pull/568#discussion_r291988770
##########
File path:
jena-arq/src/main/java/org/apache/jena/sparql/expr/aggregate/AggMedian.java
##########
@@ -70,54 +72,55 @@ public boolean equals(Aggregator other, boolean bySyntax) {
private NodeValue total = noValuesToMedian ;
private int count = 0 ;
ArrayList<NodeValue> collection=new ArrayList<NodeValue>();
-
- static final boolean DEBUG = false ;
-
+
public AccMedian(Expr expr) { super(expr, false) ; }
@Override
protected void accumulate(NodeValue nv, Binding binding, FunctionEnv
functionEnv)
{
- if ( DEBUG ) System.out.println("median: "+nv) ;
+ log.debug("median {}", nv);
if ( nv.isNumber() )
{
- count++ ;
- if ( total == noValuesToMedian )
- total = nv ;
- else
- total = XSDFuncOp.numAdd(nv, total) ;
- collection.add(nv);
+ count++ ;
+ collection.add(nv);
}
else
{
//ARQ.getExecLogger().warn("Evaluation error: median() on
"+nv) ;
throw new ExprEvalException("median: not a number: "+nv) ;
}
-
- if ( DEBUG ) System.out.println("median: ("+total+","+count+")") ;
+
+ log.debug("median count {}", count);
}
-
- @Override
- protected void accumulateError(Binding binding, FunctionEnv
functionEnv)
- {}
@Override
public NodeValue getAccValue()
{
+ double median;
if ( count == 0 ) return noValuesToMedian ;
if ( super.errorCount != 0 )
- //throw new ExprEvalException("median: error in group") ;
return null ;
- //NodeValue nvCount = NodeValue.makeInteger(count) ;
-
- double[] arrDouble = new double[collection.size()];
- for(int i=0; i<collection.size(); i++){
- arrDouble[i] = Double.parseDouble(collection.get(i).toString());
+
+ int indexsize = collection.size();
+ double[] arrDouble = new double[indexsize];
+ for(int i=0; i<indexsize; i++){
+ arrDouble[i] = collection.get(i).getDouble();
}
- System.out.println(new Median().evaluate(arrDouble));
- return (NodeValue.makeDecimal((new Median().evaluate(arrDouble
))));
+
+ Arrays.sort(arrDouble);
+
+ if(indexsize%2!=0) {
Review comment:
That's because you return an xsd:decimal : only doubles have a NaN.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services