neumarcx commented on a change in pull request #568: Add Aggregate Median to
SPARQL ARQ syntax
URL: https://github.com/apache/jena/pull/568#discussion_r291984862
##########
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:
I thought about that as well, technically it should be NaN I believe but ARQ
turns that into 0 as well. So I left it currently at the defaulting to 0 in the
NodeValue.
[test](http://www.lotico.com/query/#SELECT%20median%28%3Fx%29%0AWHERE%7B%0Avalues%20%3Fx%20%7B%7D%0A%7D)
----------------------------------------------------------------
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