use of com.facebook.presto.common.type.VarcharType in project presto by prestodb.
the class LiteralEncoder method toExpression.
@Deprecated
public Expression toExpression(Object object, Type type, boolean typeOnly) {
requireNonNull(type, "type is null");
if (object instanceof Expression) {
return (Expression) object;
}
if (object == null) {
if (type.equals(UNKNOWN)) {
return new NullLiteral();
}
return new Cast(new NullLiteral(), type.getTypeSignature().toString(), false, typeOnly);
}
if (type.equals(TINYINT)) {
return new GenericLiteral("TINYINT", object.toString());
}
if (type.equals(SMALLINT)) {
return new GenericLiteral("SMALLINT", object.toString());
}
if (type.equals(INTEGER)) {
return new LongLiteral(object.toString());
}
if (type.equals(BIGINT)) {
LongLiteral expression = new LongLiteral(object.toString());
if (expression.getValue() >= Integer.MIN_VALUE && expression.getValue() <= Integer.MAX_VALUE) {
return new GenericLiteral("BIGINT", object.toString());
}
return new LongLiteral(object.toString());
}
checkArgument(Primitives.wrap(type.getJavaType()).isInstance(object), "object.getClass (%s) and type.getJavaType (%s) do not agree", object.getClass(), type.getJavaType());
if (type.equals(DOUBLE)) {
Double value = (Double) object;
// When changing this, don't forget about similar code for REAL below
if (value.isNaN()) {
return new FunctionCall(QualifiedName.of("nan"), ImmutableList.of());
}
if (value.equals(Double.NEGATIVE_INFINITY)) {
return ArithmeticUnaryExpression.negative(new FunctionCall(QualifiedName.of("infinity"), ImmutableList.of()));
}
if (value.equals(Double.POSITIVE_INFINITY)) {
return new FunctionCall(QualifiedName.of("infinity"), ImmutableList.of());
}
return new DoubleLiteral(object.toString());
}
if (type.equals(REAL)) {
Float value = intBitsToFloat(((Long) object).intValue());
// WARNING for ORC predicate code as above (for double)
if (value.isNaN()) {
return new Cast(new FunctionCall(QualifiedName.of("nan"), ImmutableList.of()), StandardTypes.REAL);
}
if (value.equals(Float.NEGATIVE_INFINITY)) {
return ArithmeticUnaryExpression.negative(new Cast(new FunctionCall(QualifiedName.of("infinity"), ImmutableList.of()), StandardTypes.REAL));
}
if (value.equals(Float.POSITIVE_INFINITY)) {
return new Cast(new FunctionCall(QualifiedName.of("infinity"), ImmutableList.of()), StandardTypes.REAL);
}
return new GenericLiteral("REAL", value.toString());
}
if (type instanceof DecimalType) {
String string;
if (isShortDecimal(type)) {
string = Decimals.toString((long) object, ((DecimalType) type).getScale());
} else {
string = Decimals.toString((Slice) object, ((DecimalType) type).getScale());
}
return new Cast(new DecimalLiteral(string), type.getDisplayName());
}
if (type instanceof VarcharType) {
VarcharType varcharType = (VarcharType) type;
Slice value = (Slice) object;
StringLiteral stringLiteral = new StringLiteral(value.toStringUtf8());
if (!varcharType.isUnbounded() && varcharType.getLengthSafe() == SliceUtf8.countCodePoints(value)) {
return stringLiteral;
}
return new Cast(stringLiteral, type.getDisplayName(), false, typeOnly);
}
if (type instanceof CharType) {
StringLiteral stringLiteral = new StringLiteral(((Slice) object).toStringUtf8());
return new Cast(stringLiteral, type.getDisplayName(), false, typeOnly);
}
if (type.equals(BOOLEAN)) {
return new BooleanLiteral(object.toString());
}
if (type.equals(DATE)) {
return new GenericLiteral("DATE", new SqlDate(toIntExact((Long) object)).toString());
}
if (object instanceof Block) {
SliceOutput output = new DynamicSliceOutput(toIntExact(((Block) object).getSizeInBytes()));
BlockSerdeUtil.writeBlock(blockEncodingSerde, output, (Block) object);
object = output.slice();
// This if condition will evaluate to true: object instanceof Slice && !type.equals(VARCHAR)
}
if (type instanceof EnumType) {
return new EnumLiteral(type.getTypeSignature().toString(), object);
}
Signature signature = getMagicLiteralFunctionSignature(type);
if (object instanceof Slice) {
// HACK: we need to serialize VARBINARY in a format that can be embedded in an expression to be
// able to encode it in the plan that gets sent to workers.
// We do this by transforming the in-memory varbinary into a call to from_base64(<base64-encoded value>)
FunctionCall fromBase64 = new FunctionCall(QualifiedName.of("from_base64"), ImmutableList.of(new StringLiteral(VarbinaryFunctions.toBase64((Slice) object).toStringUtf8())));
return new FunctionCall(QualifiedName.of(signature.getNameSuffix()), ImmutableList.of(fromBase64));
}
Expression rawLiteral = toExpression(object, typeForMagicLiteral(type), typeOnly);
return new FunctionCall(QualifiedName.of(signature.getNameSuffix()), ImmutableList.of(rawLiteral));
}
use of com.facebook.presto.common.type.VarcharType in project presto by prestodb.
the class LiteralInterpreter method evaluate.
public static Object evaluate(ConnectorSession session, ConstantExpression node) {
Type type = node.getType();
SqlFunctionProperties properties = session.getSqlFunctionProperties();
if (node.getValue() == null) {
return null;
}
if (type instanceof BooleanType) {
return node.getValue();
}
if (type instanceof BigintType || type instanceof TinyintType || type instanceof SmallintType || type instanceof IntegerType) {
return node.getValue();
}
if (type instanceof DoubleType) {
return node.getValue();
}
if (type instanceof RealType) {
Long number = (Long) node.getValue();
return intBitsToFloat(number.intValue());
}
if (type instanceof DecimalType) {
DecimalType decimalType = (DecimalType) type;
if (decimalType.isShort()) {
checkState(node.getValue() instanceof Long);
return decodeDecimal(BigInteger.valueOf((long) node.getValue()), decimalType);
}
checkState(node.getValue() instanceof Slice);
Slice value = (Slice) node.getValue();
return decodeDecimal(decodeUnscaledValue(value), decimalType);
}
if (type instanceof VarcharType || type instanceof CharType) {
return ((Slice) node.getValue()).toStringUtf8();
}
if (type instanceof VarbinaryType) {
return new SqlVarbinary(((Slice) node.getValue()).getBytes());
}
if (type instanceof DateType) {
return new SqlDate(((Long) node.getValue()).intValue());
}
if (type instanceof TimeType) {
if (properties.isLegacyTimestamp()) {
return new SqlTime((long) node.getValue(), properties.getTimeZoneKey());
}
return new SqlTime((long) node.getValue());
}
if (type instanceof TimestampType) {
try {
if (properties.isLegacyTimestamp()) {
return new SqlTimestamp((long) node.getValue(), properties.getTimeZoneKey());
}
return new SqlTimestamp((long) node.getValue());
} catch (RuntimeException e) {
throw new PrestoException(GENERIC_USER_ERROR, format("'%s' is not a valid timestamp literal", (String) node.getValue()));
}
}
if (type instanceof IntervalDayTimeType) {
return new SqlIntervalDayTime((long) node.getValue());
}
if (type instanceof IntervalYearMonthType) {
return new SqlIntervalYearMonth(((Long) node.getValue()).intValue());
}
if (type.getJavaType().equals(Slice.class)) {
// DO NOT ever remove toBase64. Calling toString directly on Slice whose base is not byte[] will cause JVM to crash.
return "'" + VarbinaryFunctions.toBase64((Slice) node.getValue()).toStringUtf8() + "'";
}
// We should not fail at the moment; just return the raw value (block, regex, etc) to the user
return node.getValue();
}
use of com.facebook.presto.common.type.VarcharType in project presto by prestodb.
the class HiveCoercionPolicy method canCoerce.
@Override
public boolean canCoerce(HiveType fromHiveType, HiveType toHiveType) {
Type fromType = typeManager.getType(fromHiveType.getTypeSignature());
Type toType = typeManager.getType(toHiveType.getTypeSignature());
if (fromType instanceof VarcharType) {
return toHiveType.equals(HIVE_BYTE) || toHiveType.equals(HIVE_SHORT) || toHiveType.equals(HIVE_INT) || toHiveType.equals(HIVE_LONG);
}
if (toType instanceof VarcharType) {
return fromHiveType.equals(HIVE_BYTE) || fromHiveType.equals(HIVE_SHORT) || fromHiveType.equals(HIVE_INT) || fromHiveType.equals(HIVE_LONG);
}
if (fromHiveType.equals(HIVE_BYTE)) {
return toHiveType.equals(HIVE_SHORT) || toHiveType.equals(HIVE_INT) || toHiveType.equals(HIVE_LONG);
}
if (fromHiveType.equals(HIVE_SHORT)) {
return toHiveType.equals(HIVE_INT) || toHiveType.equals(HIVE_LONG);
}
if (fromHiveType.equals(HIVE_INT)) {
return toHiveType.equals(HIVE_LONG);
}
if (fromHiveType.equals(HIVE_FLOAT)) {
return toHiveType.equals(HIVE_DOUBLE);
}
return canCoerceForList(fromHiveType, toHiveType) || canCoerceForMap(fromHiveType, toHiveType) || canCoerceForStruct(fromHiveType, toHiveType);
}
use of com.facebook.presto.common.type.VarcharType in project presto by prestodb.
the class HiveCoercer method createCoercer.
static HiveCoercer createCoercer(TypeManager typeManager, HiveType fromHiveType, HiveType toHiveType) {
Type fromType = typeManager.getType(fromHiveType.getTypeSignature());
Type toType = typeManager.getType(toHiveType.getTypeSignature());
if (toType instanceof VarcharType && (fromHiveType.equals(HIVE_BYTE) || fromHiveType.equals(HIVE_SHORT) || fromHiveType.equals(HIVE_INT) || fromHiveType.equals(HIVE_LONG))) {
return new IntegerNumberToVarcharCoercer(fromType, toType);
} else if (fromType instanceof VarcharType && (toHiveType.equals(HIVE_BYTE) || toHiveType.equals(HIVE_SHORT) || toHiveType.equals(HIVE_INT) || toHiveType.equals(HIVE_LONG))) {
return new VarcharToIntegerNumberCoercer(fromType, toType);
} else if (fromHiveType.equals(HIVE_BYTE) && toHiveType.equals(HIVE_SHORT) || toHiveType.equals(HIVE_INT) || toHiveType.equals(HIVE_LONG)) {
return new IntegerNumberUpscaleCoercer(fromType, toType);
} else if (fromHiveType.equals(HIVE_SHORT) && toHiveType.equals(HIVE_INT) || toHiveType.equals(HIVE_LONG)) {
return new IntegerNumberUpscaleCoercer(fromType, toType);
} else if (fromHiveType.equals(HIVE_INT) && toHiveType.equals(HIVE_LONG)) {
return new IntegerNumberUpscaleCoercer(fromType, toType);
} else if (fromHiveType.equals(HIVE_FLOAT) && toHiveType.equals(HIVE_DOUBLE)) {
return new FloatToDoubleCoercer();
} else if (isArrayType(fromType) && isArrayType(toType)) {
return new ListCoercer(typeManager, fromHiveType, toHiveType);
} else if (isMapType(fromType) && isMapType(toType)) {
return new MapCoercer(typeManager, fromHiveType, toHiveType);
} else if (isRowType(fromType) && isRowType(toType)) {
return new StructCoercer(typeManager, fromHiveType, toHiveType);
}
throw new PrestoException(NOT_SUPPORTED, format("Unsupported coercion from %s to %s", fromHiveType, toHiveType));
}
use of com.facebook.presto.common.type.VarcharType in project presto by prestodb.
the class HiveCoercionRecordCursor method createCoercer.
private static Coercer createCoercer(TypeManager typeManager, HiveType fromHiveType, HiveType toHiveType, BridgingRecordCursor bridgingRecordCursor) {
Type fromType = typeManager.getType(fromHiveType.getTypeSignature());
Type toType = typeManager.getType(toHiveType.getTypeSignature());
if (toType instanceof VarcharType && (fromHiveType.equals(HIVE_BYTE) || fromHiveType.equals(HIVE_SHORT) || fromHiveType.equals(HIVE_INT) || fromHiveType.equals(HIVE_LONG))) {
return new IntegerNumberToVarcharCoercer();
} else if (fromType instanceof VarcharType && (toHiveType.equals(HIVE_BYTE) || toHiveType.equals(HIVE_SHORT) || toHiveType.equals(HIVE_INT) || toHiveType.equals(HIVE_LONG))) {
return new VarcharToIntegerNumberCoercer(toHiveType);
} else if (fromHiveType.equals(HIVE_BYTE) && toHiveType.equals(HIVE_SHORT) || toHiveType.equals(HIVE_INT) || toHiveType.equals(HIVE_LONG)) {
return new IntegerNumberUpscaleCoercer();
} else if (fromHiveType.equals(HIVE_SHORT) && toHiveType.equals(HIVE_INT) || toHiveType.equals(HIVE_LONG)) {
return new IntegerNumberUpscaleCoercer();
} else if (fromHiveType.equals(HIVE_INT) && toHiveType.equals(HIVE_LONG)) {
return new IntegerNumberUpscaleCoercer();
} else if (fromHiveType.equals(HIVE_FLOAT) && toHiveType.equals(HIVE_DOUBLE)) {
return new FloatToDoubleCoercer();
} else if (isArrayType(fromType) && isArrayType(toType)) {
return new ListCoercer(typeManager, fromHiveType, toHiveType, bridgingRecordCursor);
} else if (isMapType(fromType) && isMapType(toType)) {
return new MapCoercer(typeManager, fromHiveType, toHiveType, bridgingRecordCursor);
} else if (isRowType(fromType) && isRowType(toType)) {
return new StructCoercer(typeManager, fromHiveType, toHiveType, bridgingRecordCursor);
}
throw new PrestoException(NOT_SUPPORTED, format("Unsupported coercion from %s to %s", fromHiveType, toHiveType));
}
Aggregations