View Javadoc

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