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