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