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.nullcomp;
22  
23  import org.apache.expreval.client.ResultMissingColumnException;
24  import org.apache.expreval.expr.TypeSupport;
25  import org.apache.expreval.expr.node.BooleanValue;
26  import org.apache.expreval.expr.node.ByteValue;
27  import org.apache.expreval.expr.node.DateValue;
28  import org.apache.expreval.expr.node.GenericValue;
29  import org.apache.expreval.expr.node.NumberValue;
30  import org.apache.expreval.expr.node.ObjectValue;
31  import org.apache.expreval.expr.node.StringValue;
32  import org.apache.hadoop.hbase.hbql.client.HBqlException;
33  import org.apache.hadoop.hbase.hbql.impl.HConnectionImpl;
34  import org.apache.hadoop.hbase.hbql.impl.InvalidTypeException;
35  
36  public class DelegateNullCompare extends GenericNullCompare {
37  
38      public DelegateNullCompare(final boolean not, final GenericValue expr) {
39          super(null, not, expr);
40      }
41  
42      public Class<? extends GenericValue> validateTypes(final GenericValue parentExpr,
43                                                         final boolean allowCollections) throws HBqlException {
44  
45          final GenericValue genericValue = this.getExprArg(0);
46          final Class<? extends GenericValue> validatedValue = genericValue.validateTypes(this, false);
47          final Class<? extends GenericValue> type = this.getGenericValueClass(validatedValue);
48  
49          final GenericNullCompare typedExpr;
50          if (TypeSupport.isParentClass(StringValue.class, type))
51              typedExpr = new StringNullCompare(this.isNot(), this.getExprArg(0));
52          else if (TypeSupport.isParentClass(NumberValue.class, type))
53              typedExpr = new NumberNullCompare(this.isNot(), this.getExprArg(0));
54          else if (TypeSupport.isParentClass(DateValue.class, type))
55              typedExpr = new DateNullCompare(this.isNot(), this.getExprArg(0));
56          else if (TypeSupport.isParentClass(BooleanValue.class, type))
57              typedExpr = new BooleanNullCompare(this.isNot(), this.getExprArg(0));
58          else if (TypeSupport.isParentClass(ByteValue.class, type))
59              typedExpr = new ByteNullCompare(this.isNot(), this.getExprArg(0));
60          else if (TypeSupport.isParentClass(ObjectValue.class, type))
61              typedExpr = new ObjectNullCompare(this.isNot(), this.getExprArg(0));
62          else {
63              typedExpr = null;
64              throw new InvalidTypeException(this.getInvalidTypeMsg(type));
65          }
66  
67          this.setTypedExpr(typedExpr);
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          return this.getTypedExpr().getValue(conn, object);
80      }
81  }