Permalink
Browse files

float summer fn

  • Loading branch information...
1 parent aa4679e commit a6c73d67acb49c1f93caab00992d94e243faffa8 @sonalgoyal committed Nov 30, 2012
View
@@ -602,6 +602,10 @@ INSERT INTO `function`(`functionName`,`functionClass`,`aggregate`, `valueTypeId
VALUES ('Min','co.nubetech.crux.server.functions.MinAggregator',1, 4, 4);
INSERT INTO `function`(`functionName`,`functionClass`,`aggregate`, `valueTypeId`,`returnValueTypeId`)
VALUES ('Round','co.nubetech.crux.server.functions.Round',0, 6, 6);
+INSERT INTO `function`(`functionName`,`functionClass`,`aggregate`, `valueTypeId`,`returnValueTypeId`)
+ VALUES ('Sum','co.nubetech.crux.server.functions.SumDoubleAggregator',1, 6, 6);
+INSERT INTO `function`(`functionName`,`functionClass`,`aggregate`, `valueTypeId`,`returnValueTypeId`)
+ VALUES ('Sum','co.nubetech.crux.server.functions.SumFloatAggregator',1, 5, 6);
#INSERT INTO `function`(`functionName`,`functionClass`,`aggregate`, `valueTypeId`,`returnValueTypeId`) VALUES ('date','co.nubetech.crux.server.functions.DateFunction',0, 1, 3);
#INSERT INTO `function`(`functionName`,`functionClass`,`aggregate`, `valueTypeId`,`returnValueTypeId`) VALUES ('date','co.nubetech.crux.server.functions.DateFunction',0, 1, 4);
@@ -5,11 +5,11 @@
import co.nubetech.crux.util.CruxException;
-public class SumAggregator extends FunctionBase implements CruxAggregator{
+public class SumDoubleAggregator extends FunctionBase implements CruxAggregator{
private double sum;
- public SumAggregator(){
+ public SumDoubleAggregator(){
sum = 0;
}
@@ -0,0 +1,44 @@
+package co.nubetech.crux.server.functions;
+
+import org.apache.hadoop.hbase.util.Bytes;
+
+import co.nubetech.crux.util.CruxException;
+
+
+public class SumFloatAggregator extends FunctionBase implements CruxAggregator{
+
+ private double sum;
+
+ public SumFloatAggregator(){
+ sum = 0;
+ }
+
+ /*
+ * this method is invoked for each value
+ */
+ @Override
+ public void aggregate(Object o) throws CruxException{
+ if (o instanceof byte[]) {
+ Float dbl = Bytes.toFloat((byte[])o);
+ sum += dbl;
+ }
+ else {
+ Float dbl = (Float) o;
+ sum += dbl;
+ }
+ }
+
+ /*
+ * invoked in the end to get the result
+ */
+ @Override
+ public Object getAggregate() {
+ return sum;
+ }
+
+ @Override
+ public boolean isAggregate() {
+ return true;
+ }
+
+}
@@ -148,7 +148,7 @@ public void testGetAggregators() throws CruxException{
assertEquals(3, functionList.size());
Stack<CruxFunction> xFnStack = functionList.get(0);
assertEquals(2,xFnStack.size());
- assertEquals(co.nubetech.crux.server.functions.SumAggregator.class, xFnStack.pop().getClass());
+ assertEquals(co.nubetech.crux.server.functions.SumDoubleAggregator.class, xFnStack.pop().getClass());
assertEquals(co.nubetech.crux.server.functions.Ceil.class, xFnStack.pop().getClass());
Stack<CruxFunction> yFnStack = functionList.get(1);
assertEquals(1, yFnStack.size());
@@ -125,7 +125,7 @@ public static Report getReportNoGroupBy() {
ReportDesign yDesign1 = getDesign("y", alias1, report);
Function sum = getFunction("sum",
- "co.nubetech.crux.server.functions.SumAggregator", true);
+ "co.nubetech.crux.server.functions.SumDoubleAggregator", true);
Function average = getFunction("avergae", "co.nubetech.crux.server.functions.AverageAggregator",
true);
Function ceil = getFunction("ceil", "co.nubetech.crux.server.functions.Ceil", false);
@@ -201,7 +201,7 @@ public static Report getReport() {
ReportDesign yDesign1 = getDesign("y", alias1, report);
Function sum = getFunction("sum",
- "co.nubetech.crux.server.functions.SumAggregator", true);
+ "co.nubetech.crux.server.functions.SumDoubleAggregator", true);
Function average = getFunction("avergae", "co.nubetech.crux.server.functions.AverageAggregator",
true);
Function ceil = getFunction("ceil", "co.nubetech.crux.server.functions.Ceil", false);
@@ -2710,7 +2710,7 @@ public void testRangeScanCompRowAggCol() throws IOException, CruxException {
"co.nubetech.crux.server.functions.MinAggregator", true);
Function sum = TestingUtil.getFunction("sum",
- "co.nubetech.crux.server.functions.SumAggregator", true);
+ "co.nubetech.crux.server.functions.SumDoubleAggregator", true);
ReportDesign design3 = new ReportDesign();
design3.setRowAlias(rAlias1);
@@ -14,7 +14,7 @@
import co.nubetech.crux.server.functions.Ceil;
import co.nubetech.crux.server.functions.CruxFunction;
import co.nubetech.crux.server.functions.LowerCase;
-import co.nubetech.crux.server.functions.SumAggregator;
+import co.nubetech.crux.server.functions.SumDoubleAggregator;
import co.nubetech.crux.server.functions.UpperCase;
import co.nubetech.crux.util.CruxException;
@@ -23,7 +23,7 @@
@Test
public void testApplyFunctionsAggregateFirst() throws CruxException{
Stack<CruxFunction> xFnStack = new Stack<CruxFunction>();
- SumAggregator summer = new SumAggregator();
+ SumDoubleAggregator summer = new SumDoubleAggregator();
xFnStack.push(summer);
xFnStack.push(new Ceil());
byte[] value = Bytes.toBytes(new Double(54.5d));
@@ -37,7 +37,7 @@ public void testApplyFunctionsAggregateFirst() throws CruxException{
public void testApplyFunctionsNonAggregateFirst() throws CruxException{
GroupingAggregationImpl impl = new GroupingAggregationImpl();
Stack<CruxFunction> xFnStack = new Stack<CruxFunction>();
- SumAggregator summer = new SumAggregator();
+ SumDoubleAggregator summer = new SumDoubleAggregator();
xFnStack.push(new Ceil());
xFnStack.push(summer);
byte[] value = Bytes.toBytes(new Double(54.5d));
@@ -58,7 +58,7 @@ public void testApplyFunctionsAllNonAggregate() throws CruxException{
@Test
public void testApplyFunctionsOnlyAggregate() throws CruxException{
Stack<CruxFunction> xFnStack = new Stack<CruxFunction>();
- SumAggregator summer = new SumAggregator();
+ SumDoubleAggregator summer = new SumDoubleAggregator();
xFnStack.push(summer);
byte[] value = Bytes.toBytes(new Double(54.5d));
FunctionUtil.applyAggregateFunctions(value, xFnStack);
@@ -70,7 +70,7 @@ public void testApplyFunctionsOnlyAggregate() throws CruxException{
@Test
public void testGetSemiAggregatedResultNonAggregateFirst() throws CruxException{
Stack<CruxFunction> xFnStack = new Stack<CruxFunction>();
- SumAggregator summer = new SumAggregator();
+ SumDoubleAggregator summer = new SumDoubleAggregator();
summer.aggregate(Bytes.toBytes(new Double(54.5d)));
xFnStack.push(new Ceil());
xFnStack.push(summer);
@@ -80,7 +80,7 @@ public void testGetSemiAggregatedResultNonAggregateFirst() throws CruxException{
@Test
public void testGetSemiAggResultAggregateFirst() throws CruxException{
Stack<CruxFunction> xFnStack = new Stack<CruxFunction>();
- SumAggregator summer = new SumAggregator();
+ SumDoubleAggregator summer = new SumDoubleAggregator();
summer.aggregate(Bytes.toBytes(new Double(54.5d)));
xFnStack.push(summer);
xFnStack.push(new Ceil());
@@ -100,7 +100,7 @@ public void testGetSemiAggregatedResultWithoutAggFn() throws CruxException{
public void testGetSimpleFunctionResultWithAggFn() throws CruxException{
//getSimpleFunctionResult
Stack<CruxFunction> xFnStack = new Stack<CruxFunction>();
- xFnStack.push(new SumAggregator());
+ xFnStack.push(new SumDoubleAggregator());
xFnStack.push(new Ceil());
System.out.println("Value is " + FunctionUtil.getSimpleFunctionResult(Bytes.toBytes(1234.5d),
xFnStack));
@@ -12,7 +12,7 @@
import co.nubetech.crux.model.TestingUtil;
import co.nubetech.crux.server.functions.AverageAggregator;
import co.nubetech.crux.server.functions.CruxFunction;
-import co.nubetech.crux.server.functions.SumAggregator;
+import co.nubetech.crux.server.functions.SumDoubleAggregator;
import co.nubetech.crux.util.CruxException;
public class TestGABatchCallback {
@@ -7,12 +7,12 @@
import co.nubetech.crux.util.CruxException;
-public class TestSumAggregator {
+public class TestSumDoubleAggregator {
@Test
public void testSumByteArray() throws CruxException{
byte[] value1 = Bytes.toBytes(new Double(5.25d));
- SumAggregator summer = new SumAggregator();
+ SumDoubleAggregator summer = new SumDoubleAggregator();
summer.aggregate(value1);
byte[] value2 = Bytes.toBytes(new Double(25.25d));
summer.aggregate(value2);
@@ -0,0 +1,25 @@
+package co.nubetech.crux.server.functions;
+
+import static org.junit.Assert.assertEquals;
+
+import org.apache.hadoop.hbase.util.Bytes;
+import org.junit.Test;
+
+import co.nubetech.crux.util.CruxException;
+
+public class TestSumFloatAggregator {
+
+ @Test
+ public void testSumByteArray() throws CruxException{
+ byte[] value1 = Bytes.toBytes(new Float(5.25f));
+ SumFloatAggregator summer = new SumFloatAggregator();
+ summer.aggregate(value1);
+ byte[] value2 = Bytes.toBytes(new Float(25.25f));
+ summer.aggregate(value2);
+ assertEquals(30.50d, summer.getAggregate());
+ byte[] value3 = Bytes.toBytes(new Float(0f));
+ summer.aggregate(value3);
+ assertEquals(30.50d, summer.getAggregate());
+ }
+
+}

0 comments on commit a6c73d6

Please sign in to comment.