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.expreval.expr.function;
22  
23  import org.apache.expreval.client.NullColumnValueException;
24  import org.apache.expreval.client.ResultMissingColumnException;
25  import org.apache.expreval.expr.DelegateStmt;
26  import org.apache.expreval.expr.node.GenericValue;
27  import org.apache.hadoop.hbase.client.Result;
28  import org.apache.hadoop.hbase.hbql.client.HBqlException;
29  import org.apache.hadoop.hbase.hbql.impl.AggregateValue;
30  import org.apache.hadoop.hbase.hbql.impl.HConnectionImpl;
31  import org.apache.hadoop.hbase.hbql.impl.InvalidFunctionException;
32  
33  import java.util.List;
34  
35  public class DelegateFunction extends DelegateStmt<GenericFunction> {
36  
37      private final String functionName;
38  
39      public DelegateFunction(final String functionName, final List<GenericValue> exprList) {
40          super(null, exprList);
41          this.functionName = functionName;
42      }
43  
44      public Class<? extends GenericValue> validateTypes(final GenericValue parentExpr,
45                                                         final boolean allowCollections) throws HBqlException {
46  
47          final GenericFunction genericFunction = this.getFunction(this.getFunctionName(), this.getGenericValueList(), parentExpr);
48          this.setTypedExpr(genericFunction);
49          return this.getTypedExpr().validateTypes(parentExpr, false);
50      }
51  
52      public boolean isAnAggregateValue() {
53          return this.getTypedExpr().isAnAggregateValue();
54      }
55  
56      private GenericFunction getFunction(final String funcName,
57                                          final List<GenericValue> exprList,
58                                          final GenericValue parentExpr) throws InvalidFunctionException {
59  
60          GenericFunction genericFunction;
61  
62          genericFunction = GenericFunction.FunctionType.getFunction(funcName, exprList);
63          if (genericFunction != null)
64              return genericFunction;
65  
66          genericFunction = DateFunction.IntervalType.getFunction(funcName, exprList);
67          if (genericFunction != null)
68              return genericFunction;
69  
70          genericFunction = DateFunction.ConstantType.getFunction(funcName);
71          if (genericFunction != null)
72              return genericFunction;
73  
74          throw new InvalidFunctionException(funcName + " in " + parentExpr.asString());
75      }
76  
77      public GenericValue getOptimizedValue() throws HBqlException {
78          this.optimizeAllArgs();
79          return !this.isAConstant() ? this : this.getTypedExpr().getOptimizedValue();
80      }
81  
82      public Object getValue(final HConnectionImpl conn, final Object object) throws HBqlException,
83                                                                                     ResultMissingColumnException,
84                                                                                     NullColumnValueException {
85          return this.getTypedExpr().getValue(conn, object);
86      }
87  
88      public void initAggregateValue(final AggregateValue aggregateValue) throws HBqlException {
89          this.getTypedExpr().initAggregateValue(aggregateValue);
90      }
91  
92      public void applyResultToAggregateValue(final AggregateValue aggregateValue,
93                                              final Result result) throws HBqlException,
94                                                                          ResultMissingColumnException,
95                                                                          NullColumnValueException {
96          this.getTypedExpr().applyResultToAggregateValue(aggregateValue, result);
97      }
98  
99      public String getFunctionName() {
100         return this.functionName;
101     }
102 
103     public String asString() {
104         return this.getFunctionName() + super.asString();
105     }
106 }