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