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.ExpressionTree;
26  import org.apache.expreval.expr.node.BooleanValue;
27  import org.apache.expreval.expr.node.GenericValue;
28  import org.apache.expreval.expr.var.DelegateColumn;
29  import org.apache.hadoop.hbase.hbql.client.AsyncExecutorManager;
30  import org.apache.hadoop.hbase.hbql.client.HBqlException;
31  import org.apache.hadoop.hbase.hbql.client.QueryExecutorPoolManager;
32  import org.apache.hadoop.hbase.hbql.impl.HConnectionImpl;
33  import org.apache.hadoop.hbase.hbql.impl.InvalidTypeException;
34  import org.apache.hadoop.hbase.hbql.mapping.MappingContext;
35  import org.apache.hadoop.hbase.hbql.parser.ParserUtil;
36  
37  import java.util.List;
38  
39  public class BooleanFunction extends GenericFunction implements BooleanValue {
40  
41  
42      public BooleanFunction(final FunctionType functionType, final List<GenericValue> exprs) {
43          super(functionType, exprs);
44      }
45  
46      public Class<? extends GenericValue> validateTypes(final GenericValue parentExpr,
47                                                         final boolean allowCollections) throws HBqlException {
48  
49          super.validateTypes(parentExpr, allowCollections);
50  
51          switch (this.getFunctionType()) {
52  
53              case DEFINEDINROW: {
54                  if (!(this.getExprArg(0) instanceof DelegateColumn))
55                      throw new InvalidTypeException("Argument should be a column reference in: " + this.asString());
56              }
57          }
58  
59          return BooleanValue.class;
60      }
61  
62      public Boolean getValue(final HConnectionImpl conn, final Object object) throws HBqlException,
63                                                                                      ResultMissingColumnException,
64                                                                                      NullColumnValueException {
65  
66          switch (this.getFunctionType()) {
67  
68              case RANDOMBOOLEAN:
69                  return GenericFunction.randomVal.nextBoolean();
70  
71              case DEFINEDINROW:
72                  try {
73                      this.getExprArg(0).getValue(conn, object);
74                      return true;
75                  }
76                  catch (ResultMissingColumnException e) {
77                      return false;
78                  }
79  
80              case EVAL:
81                  final String exprStr = (String)this.getExprArg(0).getValue(conn, object);
82                  final MappingContext mappingContext = this.getExpressionContext().getMappingContext();
83                  final ExpressionTree expressionTree = ParserUtil.parseWhereExpression(exprStr, mappingContext);
84                  return expressionTree.evaluate(conn, object);
85  
86              case MAPPINGEXISTS:
87                  if (conn == null) {
88                      return false;
89                  }
90                  else {
91                      final String mappingName = (String)this.getExprArg(0).getValue(conn, null);
92                      return conn.mappingExists(mappingName);
93                  }
94  
95              case TABLEEXISTS:
96                  if (conn == null) {
97                      return false;
98                  }
99                  else {
100                     final String tableName = (String)this.getExprArg(0).getValue(conn, null);
101                     return conn.tableExists(tableName);
102                 }
103 
104             case TABLEENABLED:
105                 if (conn == null) {
106                     return false;
107                 }
108                 else {
109                     final String tableName = (String)this.getExprArg(0).getValue(conn, null);
110                     return conn.tableEnabled(tableName);
111                 }
112 
113             case TABLEAVAILABLE:
114                 if (conn == null) {
115                     return false;
116                 }
117                 else {
118                     final String tableName = (String)this.getExprArg(0).getValue(conn, null);
119                     return conn.tableAvailable(tableName);
120                 }
121 
122             case FAMILYEXISTSFORTABLE:
123                 if (conn == null) {
124                     return false;
125                 }
126                 else {
127                     final String familyName = (String)this.getExprArg(0).getValue(conn, null);
128                     final String tableName = (String)this.getExprArg(1).getValue(conn, null);
129                     try {
130                         return conn.familyExistsForTable(familyName, tableName);
131                     }
132                     catch (HBqlException e) {
133                         // return false if table doesn't exist
134                         return false;
135                     }
136                 }
137 
138             case FAMILYEXISTSFORMAPPING:
139                 if (conn == null) {
140                     return false;
141                 }
142                 else {
143                     final String familyName = (String)this.getExprArg(0).getValue(conn, null);
144                     final String mappingName = (String)this.getExprArg(1).getValue(conn, null);
145                     try {
146                         return conn.familyExistsForMapping(familyName, mappingName);
147                     }
148                     catch (HBqlException e) {
149                         // return false if table doesn't exist
150                         return false;
151                     }
152                 }
153 
154             case INDEXEXISTSFORTABLE:
155                 if (conn == null) {
156                     return false;
157                 }
158                 else {
159                     final String indexName = (String)this.getExprArg(0).getValue(conn, null);
160                     final String tableName = (String)this.getExprArg(1).getValue(conn, null);
161                     try {
162                         return conn.indexExistsForTable(indexName, tableName);
163                     }
164                     catch (HBqlException e) {
165                         // return false if index doesn't exist
166                         return false;
167                     }
168                 }
169 
170             case INDEXEXISTSFORMAPPING:
171                 if (conn == null) {
172                     return false;
173                 }
174                 else {
175                     final String indexName = (String)this.getExprArg(0).getValue(conn, null);
176                     final String mappingName = (String)this.getExprArg(1).getValue(conn, null);
177                     try {
178                         return conn.indexExistsForMapping(indexName, mappingName);
179                     }
180                     catch (HBqlException e) {
181                         // return false if index doesn't exist
182                         return false;
183                     }
184                 }
185 
186             case ASYNCEXECUTOREXISTS: {
187                 final String poolName = (String)this.getExprArg(0).getValue(null, null);
188                 return AsyncExecutorManager.asyncExecutorExists(poolName);
189             }
190 
191             case QUERYEXECUTORPOOLEXISTS: {
192                 final String poolName = (String)this.getExprArg(0).getValue(null, null);
193                 return QueryExecutorPoolManager.queryExecutorPoolExists(poolName);
194             }
195 
196             default:
197                 throw new HBqlException("Invalid function: " + this.getFunctionType());
198         }
199     }
200 }