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.compare;
22  
23  import org.apache.expreval.client.NullColumnValueException;
24  import org.apache.expreval.client.ResultMissingColumnException;
25  import org.apache.expreval.expr.Operator;
26  import org.apache.expreval.expr.TypeSupport;
27  import org.apache.expreval.expr.node.BooleanValue;
28  import org.apache.expreval.expr.node.ByteValue;
29  import org.apache.expreval.expr.node.DateValue;
30  import org.apache.expreval.expr.node.GenericValue;
31  import org.apache.expreval.expr.node.NumberValue;
32  import org.apache.expreval.expr.node.StringValue;
33  import org.apache.hadoop.hbase.filter.Filter;
34  import org.apache.hadoop.hbase.hbql.client.HBqlException;
35  import org.apache.hadoop.hbase.hbql.impl.HConnectionImpl;
36  import org.apache.hadoop.hbase.hbql.impl.InvalidTypeException;
37  
38  public class DelegateCompare extends GenericCompare {
39  
40      private GenericCompare typedExpr = null;
41  
42      public DelegateCompare(final GenericValue arg0, final Operator operator, final GenericValue arg1) {
43          super(arg0, operator, arg1);
44      }
45  
46      private GenericCompare getTypedExpr() {
47          return this.typedExpr;
48      }
49  
50      public Class<? extends GenericValue> validateTypes(final GenericValue parentExpr,
51                                                         final boolean allowCollections) throws HBqlException {
52  
53          final Class<? extends GenericValue> type0 = this.getExprArg(0).validateTypes(this, false);
54          final Class<? extends GenericValue> type1 = this.getExprArg(1).validateTypes(this, false);
55  
56          if (TypeSupport.isParentClass(StringValue.class, type0, type1))
57              this.typedExpr = new StringCompare(this.getExprArg(0), this.getOperator(), this.getExprArg(1));
58          else if (TypeSupport.isParentClass(NumberValue.class, type0, type1))
59              this.typedExpr = new NumberCompare(this.getExprArg(0), this.getOperator(), this.getExprArg(1));
60          else if (TypeSupport.isParentClass(DateValue.class, type0, type1))
61              this.typedExpr = new DateCompare(this.getExprArg(0), this.getOperator(), this.getExprArg(1));
62          else if (TypeSupport.isParentClass(BooleanValue.class, type0, type1))
63              this.typedExpr = new BooleanCompare(this.getExprArg(0), this.getOperator(), this.getExprArg(1));
64          else if (TypeSupport.isParentClass(ByteValue.class, type0, type1))
65              this.typedExpr = new ByteCompare(this.getExprArg(0), this.getOperator(), this.getExprArg(1));
66          else
67              throw new InvalidTypeException(this.getInvalidTypeMsg(type0, type1));
68  
69          return this.getTypedExpr().validateTypes(parentExpr, false);
70      }
71  
72      public GenericValue getOptimizedValue() throws HBqlException {
73          this.optimizeAllArgs();
74          return !this.isAConstant() ? this : this.getTypedExpr().getOptimizedValue();
75      }
76  
77      public Boolean getValue(final HConnectionImpl conn, final Object object) throws HBqlException,
78                                                                                      ResultMissingColumnException,
79                                                                                      NullColumnValueException {
80          return this.getTypedExpr().getValue(conn, object);
81      }
82  
83      public Filter getFilter() throws HBqlException {
84          return this.getTypedExpr().getFilter();
85      }
86  }