1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements.  See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership.  The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License.  You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 package org.apache.hadoop.hbase.coprocessor;
19 
20 import static org.junit.Assert.assertEquals;
21 
22 import java.math.BigDecimal;
23 
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.apache.hadoop.conf.Configuration;
27 import org.apache.hadoop.hbase.HBaseTestingUtility;
28 import org.apache.hadoop.hbase.HConstants;
29 import org.apache.hadoop.hbase.TableName;
30 import org.apache.hadoop.hbase.client.Durability;
31 import org.apache.hadoop.hbase.client.HTable;
32 import org.apache.hadoop.hbase.client.Put;
33 import org.apache.hadoop.hbase.client.Scan;
34 import org.apache.hadoop.hbase.client.coprocessor.AggregationClient;
35 import org.apache.hadoop.hbase.client.coprocessor.BigDecimalColumnInterpreter;
36 import org.apache.hadoop.hbase.filter.Filter;
37 import org.apache.hadoop.hbase.filter.PrefixFilter;
38 import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.BigDecimalMsg;
39 import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.EmptyMsg;
40 import org.apache.hadoop.hbase.testclassification.MediumTests;
41 import org.apache.hadoop.hbase.util.Bytes;
42 import org.junit.AfterClass;
43 import org.junit.BeforeClass;
44 import org.junit.Test;
45 import org.junit.experimental.categories.Category;
46 
47 /**
48  * A test class to test BigDecimalColumnInterpreter for AggregationsProtocol
49  */
50 @Category(MediumTests.class)
51 public class TestBigDecimalColumnInterpreter {
52   protected static Log myLog = LogFactory.getLog(TestBigDecimalColumnInterpreter.class);
53 
54   /**
55    * Creating the test infrastructure.
56    */
57   private static final TableName TEST_TABLE =
58       TableName.valueOf("TestTable");
59   private static final byte[] TEST_FAMILY = Bytes.toBytes("TestFamily");
60   private static final byte[] TEST_QUALIFIER = Bytes.toBytes("TestQualifier");
61   private static final byte[] TEST_MULTI_CQ = Bytes.toBytes("TestMultiCQ");
62 
63   private static byte[] ROW = Bytes.toBytes("testRow");
64   private static final int ROWSIZE = 20;
65   private static final int rowSeperator1 = 5;
66   private static final int rowSeperator2 = 12;
67   private static byte[][] ROWS = makeN(ROW, ROWSIZE);
68 
69   private static HBaseTestingUtility util = new HBaseTestingUtility();
70   private static Configuration conf = util.getConfiguration();
71 
72   /**
73    * A set up method to start the test cluster. AggregateProtocolImpl is registered and will be
74    * loaded during region startup.
75    * @throws Exception
76    */
77   @BeforeClass
setupBeforeClass()78   public static void setupBeforeClass() throws Exception {
79 
80     conf.set(CoprocessorHost.REGION_COPROCESSOR_CONF_KEY,
81       "org.apache.hadoop.hbase.coprocessor.AggregateImplementation");
82 
83     util.startMiniCluster(2);
84     final byte[][] SPLIT_KEYS = new byte[][] { ROWS[rowSeperator1], ROWS[rowSeperator2] };
85     HTable table = util.createTable(TEST_TABLE, TEST_FAMILY, SPLIT_KEYS);
86     /**
87      * The testtable has one CQ which is always populated and one variable CQ for each row rowkey1:
88      * CF:CQ CF:CQ1 rowKey2: CF:CQ CF:CQ2
89      */
90     for (int i = 0; i < ROWSIZE; i++) {
91       Put put = new Put(ROWS[i]);
92       put.setDurability(Durability.SKIP_WAL);
93       BigDecimal bd = new BigDecimal(i);
94       put.add(TEST_FAMILY, TEST_QUALIFIER, Bytes.toBytes(bd));
95       table.put(put);
96       Put p2 = new Put(ROWS[i]);
97       put.setDurability(Durability.SKIP_WAL);
98       p2.add(TEST_FAMILY, Bytes.add(TEST_MULTI_CQ, Bytes.toBytes(bd)),
99         Bytes.toBytes(bd.multiply(new BigDecimal("0.10"))));
100       table.put(p2);
101     }
102     table.close();
103   }
104 
105   /**
106    * Shutting down the cluster
107    * @throws Exception
108    */
109   @AfterClass
tearDownAfterClass()110   public static void tearDownAfterClass() throws Exception {
111     util.shutdownMiniCluster();
112   }
113 
114   /**
115    * an infrastructure method to prepare rows for the testtable.
116    * @param base
117    * @param n
118    * @return
119    */
makeN(byte[] base, int n)120   private static byte[][] makeN(byte[] base, int n) {
121     byte[][] ret = new byte[n][];
122     for (int i = 0; i < n; i++) {
123       ret[i] = Bytes.add(base, Bytes.toBytes(i));
124     }
125     return ret;
126   }
127 
128   /**
129    * ****************** Test cases for Median **********************
130    */
131   /**
132    * @throws Throwable
133    */
134   @Test (timeout=300000)
testMedianWithValidRange()135   public void testMedianWithValidRange() throws Throwable {
136     AggregationClient aClient = new AggregationClient(conf);
137     Scan scan = new Scan();
138     scan.addColumn(TEST_FAMILY, TEST_QUALIFIER);
139     final ColumnInterpreter<BigDecimal, BigDecimal, EmptyMsg, BigDecimalMsg, BigDecimalMsg> ci =
140       new BigDecimalColumnInterpreter();
141     BigDecimal median = aClient.median(TEST_TABLE, ci, scan);
142     assertEquals(new BigDecimal("8.00"), median);
143   }
144 
145   /**
146    * ***************Test cases for Maximum *******************
147    */
148 
149   /**
150    * give max for the entire table.
151    * @throws Throwable
152    */
153   @Test (timeout=300000)
testMaxWithValidRange()154   public void testMaxWithValidRange() throws Throwable {
155     AggregationClient aClient = new AggregationClient(conf);
156     Scan scan = new Scan();
157     scan.addColumn(TEST_FAMILY, TEST_QUALIFIER);
158     final ColumnInterpreter<BigDecimal, BigDecimal, EmptyMsg, BigDecimalMsg, BigDecimalMsg> ci =
159       new BigDecimalColumnInterpreter();
160     BigDecimal maximum = aClient.max(TEST_TABLE, ci, scan);
161     assertEquals(new BigDecimal("19.00"), maximum);
162   }
163 
164   /**
165    * @throws Throwable
166    */
167   @Test (timeout=300000)
testMaxWithValidRange2()168   public void testMaxWithValidRange2() throws Throwable {
169     AggregationClient aClient = new AggregationClient(conf);
170     Scan scan = new Scan();
171     scan.addColumn(TEST_FAMILY, TEST_QUALIFIER);
172     scan.setStartRow(ROWS[5]);
173     scan.setStopRow(ROWS[15]);
174     final ColumnInterpreter<BigDecimal, BigDecimal, EmptyMsg, BigDecimalMsg, BigDecimalMsg> ci =
175         new BigDecimalColumnInterpreter();
176     BigDecimal max = aClient.max(TEST_TABLE, ci, scan);
177     assertEquals(new BigDecimal("14.00"), max);
178   }
179 
180   @Test (timeout=300000)
testMaxWithValidRangeWithNoCQ()181   public void testMaxWithValidRangeWithNoCQ() throws Throwable {
182     AggregationClient aClient = new AggregationClient(conf);
183     Scan scan = new Scan();
184     scan.addFamily(TEST_FAMILY);
185     final ColumnInterpreter<BigDecimal, BigDecimal, EmptyMsg, BigDecimalMsg, BigDecimalMsg> ci =
186         new BigDecimalColumnInterpreter();
187     BigDecimal maximum = aClient.max(TEST_TABLE, ci, scan);
188     assertEquals(new BigDecimal("19.00"), maximum);
189   }
190 
191   @Test (timeout=300000)
testMaxWithValidRange2WithNoCQ()192   public void testMaxWithValidRange2WithNoCQ() throws Throwable {
193     AggregationClient aClient = new AggregationClient(conf);
194     Scan scan = new Scan();
195     scan.addFamily(TEST_FAMILY);
196     scan.setStartRow(ROWS[6]);
197     scan.setStopRow(ROWS[7]);
198     final ColumnInterpreter<BigDecimal, BigDecimal, EmptyMsg, BigDecimalMsg, BigDecimalMsg> ci =
199         new BigDecimalColumnInterpreter();
200     BigDecimal max = aClient.max(TEST_TABLE, ci, scan);
201     assertEquals(new BigDecimal("6.00"), max);
202   }
203 
204   @Test (timeout=300000)
testMaxWithValidRangeWithNullCF()205   public void testMaxWithValidRangeWithNullCF() {
206     AggregationClient aClient = new AggregationClient(conf);
207     final ColumnInterpreter<BigDecimal, BigDecimal, EmptyMsg, BigDecimalMsg, BigDecimalMsg> ci =
208       new BigDecimalColumnInterpreter();
209     Scan scan = new Scan();
210     BigDecimal max = null;
211     try {
212       max = aClient.max(TEST_TABLE, ci, scan);
213     } catch (Throwable e) {
214       max = null;
215     }
216     assertEquals(null, max);// CP will throw an IOException about the
217     // null column family, and max will be set to 0
218   }
219 
220   @Test (timeout=300000)
testMaxWithInvalidRange()221   public void testMaxWithInvalidRange() {
222     AggregationClient aClient = new AggregationClient(conf);
223     final ColumnInterpreter<BigDecimal, BigDecimal, EmptyMsg, BigDecimalMsg, BigDecimalMsg> ci =
224       new BigDecimalColumnInterpreter();
225     Scan scan = new Scan();
226     scan.setStartRow(ROWS[4]);
227     scan.setStopRow(ROWS[2]);
228     scan.addColumn(TEST_FAMILY, TEST_QUALIFIER);
229     BigDecimal max = new BigDecimal(Long.MIN_VALUE);
230     ;
231     try {
232       max = aClient.max(TEST_TABLE, ci, scan);
233     } catch (Throwable e) {
234       max = BigDecimal.ZERO;
235     }
236     assertEquals(BigDecimal.ZERO, max);// control should go to the catch block
237   }
238 
239   @Test (timeout=300000)
testMaxWithInvalidRange2()240   public void testMaxWithInvalidRange2() throws Throwable {
241     BigDecimal max = new BigDecimal(Long.MIN_VALUE);
242     Scan scan = new Scan();
243     scan.addColumn(TEST_FAMILY, TEST_QUALIFIER);
244     scan.setStartRow(ROWS[4]);
245     scan.setStopRow(ROWS[4]);
246     try {
247       AggregationClient aClient = new AggregationClient(conf);
248       final ColumnInterpreter<BigDecimal, BigDecimal, EmptyMsg, BigDecimalMsg, BigDecimalMsg> ci =
249         new BigDecimalColumnInterpreter();
250       max = aClient.max(TEST_TABLE, ci, scan);
251     } catch (Exception e) {
252       max = BigDecimal.ZERO;
253     }
254     assertEquals(BigDecimal.ZERO, max);// control should go to the catch block
255   }
256 
257   @Test (timeout=300000)
testMaxWithFilter()258   public void testMaxWithFilter() throws Throwable {
259     BigDecimal max = BigDecimal.ZERO;
260     AggregationClient aClient = new AggregationClient(conf);
261     Scan scan = new Scan();
262     scan.addColumn(TEST_FAMILY, TEST_QUALIFIER);
263     Filter f = new PrefixFilter(Bytes.toBytes("foo:bar"));
264     scan.setFilter(f);
265     final ColumnInterpreter<BigDecimal, BigDecimal, EmptyMsg, BigDecimalMsg, BigDecimalMsg> ci =
266       new BigDecimalColumnInterpreter();
267     max = aClient.max(TEST_TABLE, ci, scan);
268     assertEquals(null, max);
269   }
270 
271   /**
272    * **************************Test cases for Minimum ***********************
273    */
274 
275   /**
276    * @throws Throwable
277    */
278   @Test (timeout=300000)
testMinWithValidRange()279   public void testMinWithValidRange() throws Throwable {
280     AggregationClient aClient = new AggregationClient(conf);
281     Scan scan = new Scan();
282     scan.addColumn(TEST_FAMILY, TEST_QUALIFIER);
283     scan.setStartRow(HConstants.EMPTY_START_ROW);
284     scan.setStopRow(HConstants.EMPTY_END_ROW);
285     final ColumnInterpreter<BigDecimal, BigDecimal, EmptyMsg, BigDecimalMsg, BigDecimalMsg> ci =
286       new BigDecimalColumnInterpreter();
287     BigDecimal min = aClient.min(TEST_TABLE, ci, scan);
288     assertEquals(new BigDecimal("0.00"), min);
289   }
290 
291   /**
292    * @throws Throwable
293    */
294   @Test (timeout=300000)
testMinWithValidRange2()295   public void testMinWithValidRange2() throws Throwable {
296     AggregationClient aClient = new AggregationClient(conf);
297     Scan scan = new Scan();
298     scan.addColumn(TEST_FAMILY, TEST_QUALIFIER);
299     scan.setStartRow(ROWS[5]);
300     scan.setStopRow(ROWS[15]);
301     final ColumnInterpreter<BigDecimal, BigDecimal, EmptyMsg, BigDecimalMsg, BigDecimalMsg> ci =
302       new BigDecimalColumnInterpreter();
303     BigDecimal min = aClient.min(TEST_TABLE, ci, scan);
304     assertEquals(new BigDecimal("5.00"), min);
305   }
306 
307   @Test (timeout=300000)
testMinWithValidRangeWithNoCQ()308   public void testMinWithValidRangeWithNoCQ() throws Throwable {
309     AggregationClient aClient = new AggregationClient(conf);
310     Scan scan = new Scan();
311     scan.addFamily(TEST_FAMILY);
312     scan.setStartRow(HConstants.EMPTY_START_ROW);
313     scan.setStopRow(HConstants.EMPTY_END_ROW);
314     final ColumnInterpreter<BigDecimal, BigDecimal, EmptyMsg, BigDecimalMsg, BigDecimalMsg> ci =
315       new BigDecimalColumnInterpreter();
316     BigDecimal min = aClient.min(TEST_TABLE, ci, scan);
317     assertEquals(new BigDecimal("0.00"), min);
318   }
319 
320   @Test (timeout=300000)
testMinWithValidRange2WithNoCQ()321   public void testMinWithValidRange2WithNoCQ() throws Throwable {
322     AggregationClient aClient = new AggregationClient(conf);
323     Scan scan = new Scan();
324     scan.addFamily(TEST_FAMILY);
325     scan.setStartRow(ROWS[6]);
326     scan.setStopRow(ROWS[7]);
327     final ColumnInterpreter<BigDecimal, BigDecimal, EmptyMsg, BigDecimalMsg, BigDecimalMsg> ci =
328       new BigDecimalColumnInterpreter();
329     BigDecimal min = aClient.min(TEST_TABLE, ci, scan);
330     assertEquals(new BigDecimal("0.60"), min);
331   }
332 
333   @Test (timeout=300000)
testMinWithValidRangeWithNullCF()334   public void testMinWithValidRangeWithNullCF() {
335     AggregationClient aClient = new AggregationClient(conf);
336     Scan scan = new Scan();
337     scan.setStartRow(ROWS[5]);
338     scan.setStopRow(ROWS[15]);
339     final ColumnInterpreter<BigDecimal, BigDecimal, EmptyMsg, BigDecimalMsg, BigDecimalMsg> ci =
340       new BigDecimalColumnInterpreter();
341     BigDecimal min = null;
342     try {
343       min = aClient.min(TEST_TABLE, ci, scan);
344     } catch (Throwable e) {
345     }
346     assertEquals(null, min);// CP will throw an IOException about the
347     // null column family, and max will be set to 0
348   }
349 
350   @Test (timeout=300000)
testMinWithInvalidRange()351   public void testMinWithInvalidRange() {
352     AggregationClient aClient = new AggregationClient(conf);
353     BigDecimal min = null;
354     Scan scan = new Scan();
355     scan.addFamily(TEST_FAMILY);
356     scan.setStartRow(ROWS[4]);
357     scan.setStopRow(ROWS[2]);
358     final ColumnInterpreter<BigDecimal, BigDecimal, EmptyMsg, BigDecimalMsg, BigDecimalMsg> ci =
359       new BigDecimalColumnInterpreter();
360     try {
361       min = aClient.min(TEST_TABLE, ci, scan);
362     } catch (Throwable e) {
363     }
364     assertEquals(null, min);// control should go to the catch block
365   }
366 
367   @Test (timeout=300000)
testMinWithInvalidRange2()368   public void testMinWithInvalidRange2() {
369     AggregationClient aClient = new AggregationClient(conf);
370     Scan scan = new Scan();
371     scan.addFamily(TEST_FAMILY);
372     scan.setStartRow(ROWS[6]);
373     scan.setStopRow(ROWS[6]);
374     final ColumnInterpreter<BigDecimal, BigDecimal, EmptyMsg, BigDecimalMsg, BigDecimalMsg> ci =
375       new BigDecimalColumnInterpreter();
376     BigDecimal min = null;
377     try {
378       min = aClient.min(TEST_TABLE, ci, scan);
379     } catch (Throwable e) {
380     }
381     assertEquals(null, min);// control should go to the catch block
382   }
383 
384   @Test (timeout=300000)
testMinWithFilter()385   public void testMinWithFilter() throws Throwable {
386     AggregationClient aClient = new AggregationClient(conf);
387     Scan scan = new Scan();
388     scan.addColumn(TEST_FAMILY, TEST_QUALIFIER);
389     Filter f = new PrefixFilter(Bytes.toBytes("foo:bar"));
390     scan.setFilter(f);
391     final ColumnInterpreter<BigDecimal, BigDecimal, EmptyMsg, BigDecimalMsg, BigDecimalMsg> ci =
392       new BigDecimalColumnInterpreter();
393     BigDecimal min = null;
394     min = aClient.min(TEST_TABLE, ci, scan);
395     assertEquals(null, min);
396   }
397 
398   /**
399    * *************** Test cases for Sum *********************
400    */
401   /**
402    * @throws Throwable
403    */
404   @Test (timeout=300000)
testSumWithValidRange()405   public void testSumWithValidRange() throws Throwable {
406     AggregationClient aClient = new AggregationClient(conf);
407     Scan scan = new Scan();
408     scan.addColumn(TEST_FAMILY, TEST_QUALIFIER);
409     final ColumnInterpreter<BigDecimal, BigDecimal, EmptyMsg, BigDecimalMsg, BigDecimalMsg> ci =
410       new BigDecimalColumnInterpreter();
411     BigDecimal sum = aClient.sum(TEST_TABLE, ci, scan);
412     assertEquals(new BigDecimal("190.00"), sum);
413   }
414 
415   /**
416    * @throws Throwable
417    */
418   @Test (timeout=300000)
testSumWithValidRange2()419   public void testSumWithValidRange2() throws Throwable {
420     AggregationClient aClient = new AggregationClient(conf);
421     Scan scan = new Scan();
422     scan.addColumn(TEST_FAMILY, TEST_QUALIFIER);
423     scan.setStartRow(ROWS[5]);
424     scan.setStopRow(ROWS[15]);
425     final ColumnInterpreter<BigDecimal, BigDecimal, EmptyMsg, BigDecimalMsg, BigDecimalMsg> ci =
426       new BigDecimalColumnInterpreter();
427     BigDecimal sum = aClient.sum(TEST_TABLE, ci, scan);
428     assertEquals(new BigDecimal("95.00"), sum);
429   }
430 
431   @Test (timeout=300000)
testSumWithValidRangeWithNoCQ()432   public void testSumWithValidRangeWithNoCQ() throws Throwable {
433     AggregationClient aClient = new AggregationClient(conf);
434     Scan scan = new Scan();
435     scan.addFamily(TEST_FAMILY);
436     final ColumnInterpreter<BigDecimal, BigDecimal, EmptyMsg, BigDecimalMsg, BigDecimalMsg> ci =
437       new BigDecimalColumnInterpreter();
438     BigDecimal sum = aClient.sum(TEST_TABLE, ci, scan);
439     assertEquals(new BigDecimal("209.00"), sum); // 190 + 19
440   }
441 
442   @Test (timeout=300000)
testSumWithValidRange2WithNoCQ()443   public void testSumWithValidRange2WithNoCQ() throws Throwable {
444     AggregationClient aClient = new AggregationClient(conf);
445     Scan scan = new Scan();
446     scan.addFamily(TEST_FAMILY);
447     scan.setStartRow(ROWS[6]);
448     scan.setStopRow(ROWS[7]);
449     final ColumnInterpreter<BigDecimal, BigDecimal, EmptyMsg, BigDecimalMsg, BigDecimalMsg> ci =
450       new BigDecimalColumnInterpreter();
451     BigDecimal sum = aClient.sum(TEST_TABLE, ci, scan);
452     assertEquals(new BigDecimal("6.60"), sum); // 6 + 60
453   }
454 
455   @Test (timeout=300000)
testSumWithValidRangeWithNullCF()456   public void testSumWithValidRangeWithNullCF() {
457     AggregationClient aClient = new AggregationClient(conf);
458     Scan scan = new Scan();
459     scan.setStartRow(ROWS[6]);
460     scan.setStopRow(ROWS[7]);
461     final ColumnInterpreter<BigDecimal, BigDecimal, EmptyMsg, BigDecimalMsg, BigDecimalMsg> ci =
462       new BigDecimalColumnInterpreter();
463     BigDecimal sum = null;
464     try {
465       sum = aClient.sum(TEST_TABLE, ci, scan);
466     } catch (Throwable e) {
467     }
468     assertEquals(null, sum);// CP will throw an IOException about the
469     // null column family, and max will be set to 0
470   }
471 
472   @Test (timeout=300000)
testSumWithInvalidRange()473   public void testSumWithInvalidRange() {
474     AggregationClient aClient = new AggregationClient(conf);
475     Scan scan = new Scan();
476     scan.addFamily(TEST_FAMILY);
477     scan.setStartRow(ROWS[6]);
478     scan.setStopRow(ROWS[2]);
479     final ColumnInterpreter<BigDecimal, BigDecimal, EmptyMsg, BigDecimalMsg, BigDecimalMsg> ci =
480       new BigDecimalColumnInterpreter();
481     BigDecimal sum = null;
482     try {
483       sum = aClient.sum(TEST_TABLE, ci, scan);
484     } catch (Throwable e) {
485     }
486     assertEquals(null, sum);// control should go to the catch block
487   }
488 
489   @Test (timeout=300000)
testSumWithFilter()490   public void testSumWithFilter() throws Throwable {
491     AggregationClient aClient = new AggregationClient(conf);
492     Filter f = new PrefixFilter(Bytes.toBytes("foo:bar"));
493     Scan scan = new Scan();
494     scan.addFamily(TEST_FAMILY);
495     scan.setFilter(f);
496     final ColumnInterpreter<BigDecimal, BigDecimal, EmptyMsg, BigDecimalMsg, BigDecimalMsg> ci =
497       new BigDecimalColumnInterpreter();
498     BigDecimal sum = null;
499     sum = aClient.sum(TEST_TABLE, ci, scan);
500     assertEquals(null, sum);
501   }
502 
503   /**
504    * ****************************** Test Cases for Avg **************
505    */
506   /**
507    * @throws Throwable
508    */
509   @Test (timeout=300000)
testAvgWithValidRange()510   public void testAvgWithValidRange() throws Throwable {
511     AggregationClient aClient = new AggregationClient(conf);
512     Scan scan = new Scan();
513     scan.addColumn(TEST_FAMILY, TEST_QUALIFIER);
514     final ColumnInterpreter<BigDecimal, BigDecimal, EmptyMsg, BigDecimalMsg, BigDecimalMsg> ci =
515       new BigDecimalColumnInterpreter();
516     double avg = aClient.avg(TEST_TABLE, ci, scan);
517     assertEquals(9.5, avg, 0);
518   }
519 
520   /**
521    * @throws Throwable
522    */
523   @Test (timeout=300000)
testAvgWithValidRange2()524   public void testAvgWithValidRange2() throws Throwable {
525     AggregationClient aClient = new AggregationClient(conf);
526     Scan scan = new Scan();
527     scan.addColumn(TEST_FAMILY, TEST_QUALIFIER);
528     scan.setStartRow(ROWS[5]);
529     scan.setStopRow(ROWS[15]);
530     final ColumnInterpreter<BigDecimal, BigDecimal, EmptyMsg, BigDecimalMsg, BigDecimalMsg> ci =
531       new BigDecimalColumnInterpreter();
532     double avg = aClient.avg(TEST_TABLE, ci, scan);
533     assertEquals(9.5, avg, 0);
534   }
535 
536   @Test (timeout=300000)
testAvgWithValidRangeWithNoCQ()537   public void testAvgWithValidRangeWithNoCQ() throws Throwable {
538     AggregationClient aClient = new AggregationClient(conf);
539     Scan scan = new Scan();
540     scan.addFamily(TEST_FAMILY);
541     final ColumnInterpreter<BigDecimal, BigDecimal, EmptyMsg, BigDecimalMsg, BigDecimalMsg> ci =
542       new BigDecimalColumnInterpreter();
543     double avg = aClient.avg(TEST_TABLE, ci, scan);
544     assertEquals(10.45, avg, 0.01);
545   }
546 
547   @Test (timeout=300000)
testAvgWithValidRange2WithNoCQ()548   public void testAvgWithValidRange2WithNoCQ() throws Throwable {
549     AggregationClient aClient = new AggregationClient(conf);
550     Scan scan = new Scan();
551     scan.addFamily(TEST_FAMILY);
552     scan.setStartRow(ROWS[6]);
553     scan.setStopRow(ROWS[7]);
554     final ColumnInterpreter<BigDecimal, BigDecimal, EmptyMsg, BigDecimalMsg, BigDecimalMsg> ci =
555       new BigDecimalColumnInterpreter();
556     double avg = aClient.avg(TEST_TABLE, ci, scan);
557     assertEquals(6 + 0.60, avg, 0);
558   }
559 
560   @Test (timeout=300000)
testAvgWithValidRangeWithNullCF()561   public void testAvgWithValidRangeWithNullCF() {
562     AggregationClient aClient = new AggregationClient(conf);
563     Scan scan = new Scan();
564     final ColumnInterpreter<BigDecimal, BigDecimal, EmptyMsg, BigDecimalMsg, BigDecimalMsg> ci =
565       new BigDecimalColumnInterpreter();
566     Double avg = null;
567     try {
568       avg = aClient.avg(TEST_TABLE, ci, scan);
569     } catch (Throwable e) {
570     }
571     assertEquals(null, avg);// CP will throw an IOException about the
572     // null column family, and max will be set to 0
573   }
574 
575   @Test (timeout=300000)
testAvgWithInvalidRange()576   public void testAvgWithInvalidRange() {
577     AggregationClient aClient = new AggregationClient(conf);
578     Scan scan = new Scan();
579     scan.addColumn(TEST_FAMILY, TEST_QUALIFIER);
580     scan.setStartRow(ROWS[5]);
581     scan.setStopRow(ROWS[1]);
582     final ColumnInterpreter<BigDecimal, BigDecimal, EmptyMsg, BigDecimalMsg, BigDecimalMsg> ci =
583       new BigDecimalColumnInterpreter();
584     Double avg = null;
585     try {
586       avg = aClient.avg(TEST_TABLE, ci, scan);
587     } catch (Throwable e) {
588     }
589     assertEquals(null, avg);// control should go to the catch block
590   }
591 
592   @Test (timeout=300000)
testAvgWithFilter()593   public void testAvgWithFilter() throws Throwable {
594     AggregationClient aClient = new AggregationClient(conf);
595     Scan scan = new Scan();
596     scan.addColumn(TEST_FAMILY, TEST_QUALIFIER);
597     Filter f = new PrefixFilter(Bytes.toBytes("foo:bar"));
598     scan.setFilter(f);
599     final ColumnInterpreter<BigDecimal, BigDecimal, EmptyMsg, BigDecimalMsg, BigDecimalMsg> ci =
600       new BigDecimalColumnInterpreter();
601     Double avg = null;
602     avg = aClient.avg(TEST_TABLE, ci, scan);
603     assertEquals(Double.NaN, avg, 0);
604   }
605 
606   /**
607    * ****************** Test cases for STD **********************
608    */
609   /**
610    * @throws Throwable
611    */
612   @Test (timeout=300000)
testStdWithValidRange()613   public void testStdWithValidRange() throws Throwable {
614     AggregationClient aClient = new AggregationClient(conf);
615     Scan scan = new Scan();
616     scan.addColumn(TEST_FAMILY, TEST_QUALIFIER);
617     final ColumnInterpreter<BigDecimal, BigDecimal, EmptyMsg, BigDecimalMsg, BigDecimalMsg> ci =
618       new BigDecimalColumnInterpreter();
619     double std = aClient.std(TEST_TABLE, ci, scan);
620     assertEquals(5.766, std, 0.05d);
621   }
622 
623   /**
624    * need to change this
625    * @throws Throwable
626    */
627   @Test (timeout=300000)
testStdWithValidRange2()628   public void testStdWithValidRange2() throws Throwable {
629     AggregationClient aClient = new AggregationClient(conf);
630     Scan scan = new Scan();
631     scan.addColumn(TEST_FAMILY, TEST_QUALIFIER);
632     scan.setStartRow(ROWS[5]);
633     scan.setStopRow(ROWS[15]);
634     final ColumnInterpreter<BigDecimal, BigDecimal, EmptyMsg, BigDecimalMsg, BigDecimalMsg> ci =
635       new BigDecimalColumnInterpreter();
636     double std = aClient.std(TEST_TABLE, ci, scan);
637     assertEquals(2.87, std, 0.05d);
638   }
639 
640   /**
641    * need to change this
642    * @throws Throwable
643    */
644   @Test (timeout=300000)
testStdWithValidRangeWithNoCQ()645   public void testStdWithValidRangeWithNoCQ() throws Throwable {
646     AggregationClient aClient = new AggregationClient(conf);
647     Scan scan = new Scan();
648     scan.addFamily(TEST_FAMILY);
649     final ColumnInterpreter<BigDecimal, BigDecimal, EmptyMsg, BigDecimalMsg, BigDecimalMsg> ci =
650       new BigDecimalColumnInterpreter();
651     double std = aClient.std(TEST_TABLE, ci, scan);
652     assertEquals(6.342, std, 0.05d);
653   }
654 
655   @Test (timeout=300000)
testStdWithValidRange2WithNoCQ()656   public void testStdWithValidRange2WithNoCQ() throws Throwable {
657     AggregationClient aClient = new AggregationClient(conf);
658     Scan scan = new Scan();
659     scan.addFamily(TEST_FAMILY);
660     scan.setStartRow(ROWS[6]);
661     scan.setStopRow(ROWS[7]);
662     final ColumnInterpreter<BigDecimal, BigDecimal, EmptyMsg, BigDecimalMsg, BigDecimalMsg> ci =
663       new BigDecimalColumnInterpreter();
664     double std = aClient.std(TEST_TABLE, ci, scan);
665     System.out.println("std is:" + std);
666     assertEquals(0, std, 0.05d);
667   }
668 
669   @Test (timeout=300000)
testStdWithValidRangeWithNullCF()670   public void testStdWithValidRangeWithNullCF() {
671     AggregationClient aClient = new AggregationClient(conf);
672     Scan scan = new Scan();
673     scan.setStartRow(ROWS[6]);
674     scan.setStopRow(ROWS[17]);
675     final ColumnInterpreter<BigDecimal, BigDecimal, EmptyMsg, BigDecimalMsg, BigDecimalMsg> ci =
676       new BigDecimalColumnInterpreter();
677     Double std = null;
678     try {
679       std = aClient.std(TEST_TABLE, ci, scan);
680     } catch (Throwable e) {
681     }
682     assertEquals(null, std);// CP will throw an IOException about the
683     // null column family, and max will be set to 0
684   }
685 
686   @Test
testStdWithInvalidRange()687   public void testStdWithInvalidRange() {
688     AggregationClient aClient = new AggregationClient(conf);
689     Scan scan = new Scan();
690     scan.addFamily(TEST_FAMILY);
691     scan.setStartRow(ROWS[6]);
692     scan.setStopRow(ROWS[1]);
693     final ColumnInterpreter<BigDecimal, BigDecimal, EmptyMsg, BigDecimalMsg, BigDecimalMsg> ci =
694       new BigDecimalColumnInterpreter();
695     Double std = null;
696     try {
697       std = aClient.std(TEST_TABLE, ci, scan);
698     } catch (Throwable e) {
699     }
700     assertEquals(null, std);// control should go to the catch block
701   }
702 
703   @Test (timeout=300000)
testStdWithFilter()704   public void testStdWithFilter() throws Throwable {
705     AggregationClient aClient = new AggregationClient(conf);
706     Filter f = new PrefixFilter(Bytes.toBytes("foo:bar"));
707     Scan scan = new Scan();
708     scan.addFamily(TEST_FAMILY);
709     scan.setFilter(f);
710     final ColumnInterpreter<BigDecimal, BigDecimal, EmptyMsg, BigDecimalMsg, BigDecimalMsg> ci =
711         new BigDecimalColumnInterpreter();
712     Double std = null;
713     std = aClient.std(TEST_TABLE, ci, scan);
714     assertEquals(Double.NaN, std, 0);
715   }
716 }