Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
ValueRange.java
Go to the documentation of this file.
1 // Copyright 2012 Cloudera Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 package com.cloudera.impala.planner;
16 
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19 
28 import com.google.common.base.Preconditions;
29 
35 public class ValueRange {
36  private final static Logger LOG = LoggerFactory.getLogger(ValueRange.class);
37 
38  private Expr lowerBound_;
39  private boolean lowerBoundInclusive_;
40  private Expr upperBound_;
41  private boolean upperBoundInclusive_;
42 
44  void setLowerBound(Expr e) { lowerBound_ = e; }
48  void setUpperBound(Expr e) { upperBound_ = e; }
51 
52  static public ValueRange createEqRange(Expr valueExpr) {
53  ValueRange result = new ValueRange();
54  result.lowerBound_ = valueExpr;
55  result.lowerBoundInclusive_ = true;
56  result.upperBound_ = valueExpr;
57  result.upperBoundInclusive_ = true;
58  return result;
59  }
60 
61  public boolean isEqRange() {
63  }
64 
71  public boolean isInRange(Analyzer analyzer, Expr valueExpr) throws
73  Preconditions.checkState(valueExpr.isConstant());
74  Preconditions.checkState(lowerBound_ != null || upperBound_ != null);
75 
76  // construct predicate
77  Predicate p = null;
78  if (lowerBound_ != null && upperBound_ != null
80  && lowerBound_ == upperBound_) {
81  // construct "=" predicate
82  p = new BinaryPredicate(BinaryPredicate.Operator.EQ, valueExpr, lowerBound_);
83  } else {
84  // construct range predicate
85  if (lowerBound_ != null) {
86  p = new BinaryPredicate(
88  ? BinaryPredicate.Operator.GE : BinaryPredicate.Operator.GT,
89  valueExpr, lowerBound_);
90  }
91  if (upperBound_ != null) {
92  Predicate p2 = new BinaryPredicate(
94  ? BinaryPredicate.Operator.GE : BinaryPredicate.Operator.GT,
95  upperBound_, valueExpr);
96  if (p != null) {
97  p = new CompoundPredicate(CompoundPredicate.Operator.AND, p, p2);
98  } else {
99  p = p2;
100  }
101  }
102  }
103 
104  Preconditions.checkState(p.isConstant());
105  // analyze to insert casts, etc.
106  try {
107  p.analyze(analyzer);
108  } catch (AnalysisException e) {
109  // this should never happen
110  throw new InternalException(
111  "couldn't analyze predicate " + p.toSql() + "\n" + e.toString());
112  }
113 
114  // call backend
115  return FeSupport.EvalPredicate(p, analyzer.getQueryCtx());
116  }
117 
118 }
static ValueRange createEqRange(Expr valueExpr)
Definition: ValueRange.java:52
boolean isInRange(Analyzer analyzer, Expr valueExpr)
Definition: ValueRange.java:71