15 package com.cloudera.impala.analysis;
17 import java.math.BigDecimal;
22 import com.google.common.base.Preconditions;
53 Preconditions.checkState(decType.isFullySpecifiedDecimal());
54 Preconditions.checkState(decType.decimalScale() == 0);
72 Preconditions.checkState(t1.isDecimal());
73 Preconditions.checkState(t2.isDecimal());
78 Preconditions.checkState(t1.isFullySpecifiedDecimal());
79 Preconditions.checkState(t2.isFullySpecifiedDecimal());
80 if (t1.equals(t2))
return t1;
81 int s1 = t1.decimalScale();
82 int s2 = t2.decimalScale();
83 int p1 = t1.decimalPrecision();
84 int p2 = t2.decimalPrecision();
85 int digitsBefore = Math.max(p1 - s1, p2 - s2);
86 int digitsAfter = Math.max(s1, s2);
87 return ScalarType.createDecimalTypeInternal(
88 digitsBefore + digitsAfter, digitsAfter);
97 Preconditions.checkState(t1.isNumericType() || t1.
isNull());
98 Preconditions.checkState(t2.isNumericType() || t2.
isNull());
103 if (t1.
isNull())
return t2;
104 if (t2.
isNull())
return t1;
106 t1 = ((
ScalarType) t1).getMinResolutionDecimal();
107 t2 = ((
ScalarType) t2).getMinResolutionDecimal();
108 Preconditions.checkState(t1.isDecimal());
109 Preconditions.checkState(t2.isDecimal());
121 Type compatibleType =
122 ScalarType.getAssignmentCompatibleType(t1, t2);
123 Preconditions.checkState(compatibleType.isScalarType());
124 type = ((
ScalarType) compatibleType).getNextResolutionType();
127 type = ScalarType.getAssignmentCompatibleType(t1, t2);
133 throw new AnalysisException(
"Invalid op: " + op);
135 Preconditions.checkState(type.isValid());
150 Preconditions.checkState(t1.isFullySpecifiedDecimal());
151 Preconditions.checkState(t2.isFullySpecifiedDecimal());
154 int s1 = st1.decimalScale();
155 int s2 = st2.decimalScale();
156 int p1 = st1.decimalPrecision();
157 int p2 = st2.decimalPrecision();
158 int sMax = Math.max(s1, s2);
163 return ScalarType.createDecimalTypeInternal(
164 sMax + Math.max(p1 - s1, p2 - s2) + 1, sMax);
166 return ScalarType.createDecimalTypeInternal(p1 + p2, s1 + s2);
169 int resultPrecision = p1 - s1 + s2 + resultScale;
177 resultScale = Math.max(s1, s2);
180 return ScalarType.createDecimalTypeInternal(resultPrecision, resultScale);
182 return ScalarType.createDecimalTypeInternal(
183 Math.min(p1 - s1, p2 - s2) + sMax, sMax);
185 throw new AnalysisException(
186 "Operation '" + op +
"' is not allowed for decimal types.");
200 String str = v.toPlainString();
201 int digitsBefore = 0;
203 boolean decimalFound =
false;
204 boolean leadingZeros =
true;
205 for (
int i = 0; i < str.length(); ++i) {
206 char c = str.charAt(i);
207 if (c ==
'-')
continue;
217 if (c ==
'0' && leadingZeros)
continue;
218 leadingZeros =
false;
224 if (digitsBefore == 0 && digitsAfter == 0) digitsBefore = 1;
225 return ScalarType.createDecimalType(digitsBefore + digitsAfter, digitsAfter);
boolean isWildcardDecimal()
static final ScalarType NULL
static final ScalarType BIGINT
static final int MAX_PRECISION
static Type getArithmeticResultType(Type t1, Type t2, ArithmeticExpr.Operator op)
static ScalarType getDecimalAssignmentCompatibleType(ScalarType t1, ScalarType t2)
static final int MAX_SCALE
static int getDecimalSlotSize(ScalarType type)
static final ScalarType SMALLINT
static ScalarType getContainingIntType(ScalarType decType)
static ScalarType getDecimalArithmeticResultType(Type t1, Type t2, ArithmeticExpr.Operator op)
static final ScalarType DOUBLE
static final ScalarType TINYINT
static final ScalarType INT
static Type computeDecimalType(BigDecimal v)
static final int DECIMAL_DIVISION_SCALE_INCREMENT