15 package com.cloudera.impala.analysis;
26 import com.cloudera.impala.thrift.TExpr;
27 import com.cloudera.impala.thrift.TExprNode;
28 import com.cloudera.impala.thrift.TExprNodeType;
29 import com.google.common.base.Objects;
30 import com.google.common.base.Preconditions;
31 import com.google.common.collect.Lists;
48 Preconditions.checkState(targetType.isValid());
49 Preconditions.checkNotNull(e);
56 if (castExpr.
isImplicit()) e = castExpr.getChild(0);
67 Preconditions.checkState(
false,
68 "Implicit casts should never throw analysis exception.");
77 Preconditions.checkNotNull(targetTypeDef);
78 Preconditions.checkNotNull(e);
95 return "castTo" + targetType.getPrimitiveType().
toString();
100 if (fromType.isNull())
continue;
102 if (toType.isNull())
continue;
104 if (fromType.isStringType() && toType.isBoolean())
continue;
106 if ((fromType.isBoolean() || fromType.isDateType()) && toType.isDecimal()) {
109 if (fromType.getPrimitiveType() == PrimitiveType.STRING
112 String beSymbol =
"impala::CastFunctions::CastToChar";
115 beSymbol, null, null,
true));
118 if (fromType.getPrimitiveType() == PrimitiveType.CHAR
121 String beSymbol =
"impala::CastFunctions::CastToChar";
127 if (fromType.getPrimitiveType() == PrimitiveType.VARCHAR
130 String beSymbol =
"impala::CastFunctions::CastToStringVal";
133 beSymbol, null, null,
true));
136 if (fromType.getPrimitiveType() == PrimitiveType.VARCHAR
139 String beSymbol =
"impala::CastFunctions::CastToChar";
142 beSymbol, null, null,
true));
145 if (fromType.getPrimitiveType() == PrimitiveType.CHAR
148 String beSymbol =
"impala::CastFunctions::CastToStringVal";
151 beSymbol, null, null,
true));
155 if (fromType.equals(toType) && !fromType.isDecimal())
continue;
156 String beClass = toType.isDecimal() || fromType.isDecimal() ?
157 "DecimalOperators" :
"CastFunctions";
158 String beSymbol =
"impala::" + beClass +
"::CastTo" + Function.getUdfType(toType);
159 db.addBuiltin(ScalarFunction.createBuiltin(
getFnName(toType),
160 Lists.newArrayList(fromType),
false, toType, beSymbol,
175 getChild(0).treeToThriftHelper(container);
178 super.treeToThriftHelper(container);
183 msg.node_type = TExprNodeType.FUNCTION_CALL;
188 return Objects.toStringHelper(
this)
190 .add(
"target",
type_)
191 .addValue(super.debugString())
201 super.analyze(analyzer);
202 targetTypeDef_.analyze(analyzer);
203 type_ = targetTypeDef_.getType();
208 Preconditions.checkNotNull(
type_);
210 throw new AnalysisException(
211 "Unsupported cast to complex type: " +
type_.
toSql());
214 boolean readyForCharCast =
224 children_.set(0, tostring);
227 if (children_.get(0) instanceof
NumericLiteral && type_.isFloatingPointType()) {
234 if (children_.get(0).getType().isNull()) {
243 nullChild.uncheckedCastTo(
type_);
247 Preconditions.checkState(!childType.isNull());
248 if (childType.equals(
type_)) {
254 Type[] args = { childType };
258 Preconditions.checkState(
fn_ != null);
263 fn_ = Catalog.getBuiltin(searchDesc.promoteCharsToStrings(),
268 throw new AnalysisException(
"Invalid type cast of " + getChild(0).
toSql() +
269 " from " + childType +
" to " +
type_);
272 Preconditions.checkState(type_.matchesType(fn_.getReturnType()),
273 type_ +
" != " + fn_.getReturnType());
283 Preconditions.checkState(
293 if (
this == obj)
return true;
297 && type_.equals(other.type_)
298 && super.equals(obj);
void uncheckedCastChild(Type targetType, int childIndex)
void computeNumDistinctValues()
static final ScalarType CHAR
final boolean isImplicit_
static final ScalarType VARCHAR
static final ScalarType STRING
final TypeDef targetTypeDef_
PrimitiveType getPrimitiveType()
static final String BUILTINS_DB
static void initBuiltins(Db db)
static ScalarType createCharType(int len)
CastExpr(TypeDef targetTypeDef, Expr e)
boolean equals(Object obj)
void analyze(Analyzer analyzer)
static String getFnName(Type targetType)
void treeToThriftHelper(TExpr container)
void toThrift(TExprNode msg)
Expr ignoreImplicitCast()
static final ScalarType INVALID
static ArrayList< ScalarType > getSupportedTypes()
CastExpr(Type targetType, Expr e)