View Javadoc

1   /*
2    * Copyright (c) 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  
21  package org.apache.hadoop.hbase.hbql.impl;
22  
23  import org.apache.expreval.client.NullColumnValueException;
24  import org.apache.expreval.client.ResultMissingColumnException;
25  import org.apache.hadoop.hbase.client.Result;
26  import org.apache.hadoop.hbase.hbql.client.HBqlException;
27  import org.apache.hadoop.hbase.hbql.mapping.MappingContext;
28  import org.apache.hadoop.hbase.hbql.statement.SelectStatement;
29  import org.apache.hadoop.hbase.hbql.statement.select.SelectElement;
30  
31  import java.util.List;
32  
33  public class AggregateRecord extends HRecordImpl {
34  
35      final List<SelectElement> selectElementList;
36  
37      private AggregateRecord(final MappingContext mappingContext,
38                              final List<SelectElement> selectElementList) throws HBqlException {
39          super(mappingContext);
40  
41          this.selectElementList = selectElementList;
42  
43          // Set key value
44          this.getTableMapping().getKeyAttrib().setCurrentValue(this, 0, "");
45  
46          for (final SelectElement selectElement : this.getSelectElementList()) {
47              final AggregateValue val = selectElement.newAggregateValue();
48              val.initAggregateValue();
49              this.addElement(val);
50          }
51      }
52  
53      public static AggregateRecord newAggregateRecord(final SelectStatement selectStmt) throws HBqlException {
54          return (selectStmt.isAnAggregateQuery())
55                 ? new AggregateRecord(selectStmt.getMappingContext(), selectStmt.getSelectElementList())
56                 : null;
57      }
58  
59      private List<SelectElement> getSelectElementList() {
60          return this.selectElementList;
61      }
62  
63      public void applyValues(final Result result) throws HBqlException {
64  
65          for (final ColumnValue val : this.getColumnValuesMap().values()) {
66              if (val instanceof AggregateValue) {
67                  final AggregateValue aggregateValue = (AggregateValue)val;
68                  try {
69                      aggregateValue.applyValues(result);
70                  }
71                  catch (ResultMissingColumnException e) {
72                      // no op
73                  }
74                  catch (NullColumnValueException e) {
75                      // no op
76                  }
77              }
78          }
79      }
80  }