Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
BoolLiteral.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.analysis;
16 
19 import com.cloudera.impala.thrift.TBoolLiteral;
20 import com.cloudera.impala.thrift.TExprNode;
21 import com.cloudera.impala.thrift.TExprNodeType;
22 import com.google.common.base.Objects;
23 
24 public class BoolLiteral extends LiteralExpr {
25  private final boolean value_;
26 
27  public BoolLiteral(boolean value) {
28  this.value_ = value;
30  }
31 
32  public BoolLiteral(String value) throws AnalysisException {
34  if (value.toLowerCase().equals("true")) {
35  this.value_ = true;
36  } else if (value.toLowerCase().equals("false")) {
37  this.value_ = false;
38  } else {
39  throw new AnalysisException("invalid BOOLEAN literal: " + value);
40  }
41  }
42 
46  protected BoolLiteral(BoolLiteral other) {
47  super(other);
48  value_ = other.value_;
49  }
50 
51  @Override
52  public String debugString() {
53  return Objects.toStringHelper(this)
54  .add("value", value_)
55  .toString();
56  }
57 
58  @Override
59  public boolean equals(Object obj) {
60  if (!super.equals(obj)) {
61  return false;
62  }
63  return ((BoolLiteral) obj).value_ == value_;
64  }
65 
66  public boolean getValue() { return value_; }
67 
68  @Override
69  public String toSqlImpl() {
70  return getStringValue();
71  }
72 
73  @Override
74  public String getStringValue() {
75  return value_ ? "TRUE" : "FALSE";
76  }
77 
78  @Override
79  protected void toThrift(TExprNode msg) {
80  msg.node_type = TExprNodeType.BOOL_LITERAL;
81  msg.bool_literal = new TBoolLiteral(value_);
82  }
83 
84  @Override
85  protected Expr uncheckedCastTo(Type targetType) throws AnalysisException {
86  if (targetType.equals(this.type_)) {
87  return this;
88  } else {
89  return new CastExpr(targetType, this);
90  }
91  }
92 
93  @Override
94  public int compareTo(LiteralExpr o) {
95  int ret = super.compareTo(o);
96  if (ret != 0) return ret;
97  BoolLiteral other = (BoolLiteral) o;
98  if (value_ && !other.getValue()) return 1;
99  if (!value_ && other.getValue()) return -1;
100  return 0;
101  }
102 
103  @Override
104  public Expr clone() { return new BoolLiteral(this); }
105 }
static final ScalarType BOOLEAN
Definition: Type.java:46