15 package com.cloudera.impala.analysis;
17 import java.math.BigDecimal;
18 import java.math.BigInteger;
24 import com.cloudera.impala.thrift.TDecimalLiteral;
25 import com.cloudera.impala.thrift.TExprNode;
26 import com.cloudera.impala.thrift.TExprNodeType;
27 import com.cloudera.impala.thrift.TFloatLiteral;
28 import com.cloudera.impala.thrift.TIntLiteral;
29 import com.google.common.base.Objects;
30 import com.google.common.base.Preconditions;
57 BigDecimal val = null;
59 val =
new BigDecimal(value);
60 }
catch (NumberFormatException e) {
70 StringBuilder errMsg =
new StringBuilder();
71 errMsg.append(
"invalid ").append(t);
72 errMsg.append(
" value: " + value);
86 value_ =
new BigDecimal(value);
109 return Objects.toStringHelper(
this)
117 if (!super.equals(obj))
return false;
136 msg.node_type = TExprNodeType.INT_LITERAL;
137 msg.int_literal =
new TIntLiteral(
value_.longValue());
141 msg.node_type = TExprNodeType.FLOAT_LITERAL;
142 msg.float_literal =
new TFloatLiteral(
value_.doubleValue());
145 msg.node_type = TExprNodeType.DECIMAL_LITERAL;
146 TDecimalLiteral literal =
new TDecimalLiteral();
148 msg.decimal_literal = literal;
151 Preconditions.checkState(
false);
160 super.analyze(analyzer);
165 Double d =
new Double(
value_.doubleValue());
166 if (d.isInfinite()) {
168 "' exceeds maximum range of doubles.");
169 }
else if (d.doubleValue() == 0 &&
value_ != BigDecimal.ZERO) {
171 "' underflows minimum resolution of doubles.");
177 fvalue = value_.floatValue();
178 if (fvalue ==
value_.doubleValue()) {
185 Preconditions.checkState(type_.isScalarType());
188 if (
value_.compareTo(BigDecimal.valueOf(Byte.MAX_VALUE)) <= 0 &&
189 value_.compareTo(BigDecimal.valueOf(Byte.MIN_VALUE)) >= 0) {
191 }
else if (
value_.compareTo(BigDecimal.valueOf(Short.MAX_VALUE)) <= 0 &&
192 value_.compareTo(BigDecimal.valueOf(Short.MIN_VALUE)) >= 0) {
194 }
else if (
value_.compareTo(BigDecimal.valueOf(Integer.MAX_VALUE)) <= 0 &&
195 value_.compareTo(BigDecimal.valueOf(Integer.MIN_VALUE)) >= 0) {
197 }
else if (
value_.compareTo(BigDecimal.valueOf(Long.MAX_VALUE)) <= 0 &&
198 value_.compareTo(BigDecimal.valueOf(Long.MIN_VALUE)) >= 0) {
212 Preconditions.checkState(targetType.isFloatingPointType());
219 Preconditions.checkState(targetType.isNumericType());
227 if (targetType.isDecimal()) {
233 Preconditions.checkState(value_.precision() <= decimalType.
decimalPrecision());
234 Preconditions.checkState(value_.scale() <= decimalType.
decimalScale());
235 int valLeftDigits = value_.precision() -
value_.scale();
236 int typeLeftDigits = decimalType.decimalPrecision() - decimalType.
decimalScale();
237 if (typeLeftDigits < valLeftDigits)
return new CastExpr(targetType,
this);
251 int ret = super.compareTo(o);
252 if (ret != 0)
return ret;
254 return value_.compareTo(other.value_);
257 private void init(BigDecimal value) {
265 Preconditions.checkState(type_.isDecimal());
266 BigInteger result = value_.unscaledValue();
267 int valueScale = value_.scale();
272 return result.multiply(BigInteger.TEN.pow(decimalType.decimalScale() - valueScale));
NumericLiteral(BigDecimal value, Type type)
NumericLiteral(BigInteger value, Type type)
NumericLiteral(NumericLiteral other)
static final ScalarType BIGINT
void init(BigDecimal value)
PrimitiveType getPrimitiveType()
boolean isSupertypeOf(ScalarType o)
NumericLiteral(String value, Type t)
void analyze(Analyzer analyzer)
void explicitlyCastToFloat(Type targetType)
static final ScalarType SMALLINT
static final ScalarType FLOAT
int compareTo(LiteralExpr o)
BigInteger getUnscaledValue()
void toThrift(TExprNode msg)
static final ScalarType DOUBLE
static final ScalarType TINYINT
static final ScalarType INT
NumericLiteral(BigDecimal value)
boolean equals(Object obj)
Expr uncheckedCastTo(Type targetType)