1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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 }