use of com.facebook.presto.spi.type.VarcharType in project presto by prestodb.
the class HiveWriteUtils method getJavaObjectInspector.
public static ObjectInspector getJavaObjectInspector(Type type) {
if (type.equals(BooleanType.BOOLEAN)) {
return javaBooleanObjectInspector;
} else if (type.equals(BigintType.BIGINT)) {
return javaLongObjectInspector;
} else if (type.equals(IntegerType.INTEGER)) {
return javaIntObjectInspector;
} else if (type.equals(SmallintType.SMALLINT)) {
return javaShortObjectInspector;
} else if (type.equals(TinyintType.TINYINT)) {
return javaByteObjectInspector;
} else if (type.equals(RealType.REAL)) {
return javaFloatObjectInspector;
} else if (type.equals(DoubleType.DOUBLE)) {
return javaDoubleObjectInspector;
} else if (type instanceof VarcharType) {
return writableStringObjectInspector;
} else if (type instanceof CharType) {
return writableHiveCharObjectInspector;
} else if (type.equals(VarbinaryType.VARBINARY)) {
return javaByteArrayObjectInspector;
} else if (type.equals(DateType.DATE)) {
return javaDateObjectInspector;
} else if (type.equals(TimestampType.TIMESTAMP)) {
return javaTimestampObjectInspector;
} else if (type instanceof DecimalType) {
DecimalType decimalType = (DecimalType) type;
return getPrimitiveJavaObjectInspector(new DecimalTypeInfo(decimalType.getPrecision(), decimalType.getScale()));
} else if (isArrayType(type)) {
return ObjectInspectorFactory.getStandardListObjectInspector(getJavaObjectInspector(type.getTypeParameters().get(0)));
} else if (isMapType(type)) {
ObjectInspector keyObjectInspector = getJavaObjectInspector(type.getTypeParameters().get(0));
ObjectInspector valueObjectInspector = getJavaObjectInspector(type.getTypeParameters().get(1));
return ObjectInspectorFactory.getStandardMapObjectInspector(keyObjectInspector, valueObjectInspector);
} else if (isRowType(type)) {
return ObjectInspectorFactory.getStandardStructObjectInspector(type.getTypeSignature().getParameters().stream().map(parameter -> parameter.getNamedTypeSignature().getName()).collect(toList()), type.getTypeParameters().stream().map(HiveWriteUtils::getJavaObjectInspector).collect(toList()));
}
throw new IllegalArgumentException("unsupported type: " + type);
}
use of com.facebook.presto.spi.type.VarcharType in project presto by prestodb.
the class TestExpressionCompiler method testFunctionCall.
@Test
public void testFunctionCall() throws Exception {
for (Integer left : intLefts) {
for (Integer right : intRights) {
assertExecute(generateExpression("log(%s, %s)", left, right), DOUBLE, left == null || right == null ? null : MathFunctions.log(left, right));
}
}
for (Integer left : intLefts) {
for (Double right : doubleRights) {
assertExecute(generateExpression("log(%s, %s)", left, right), DOUBLE, left == null || right == null ? null : MathFunctions.log(left, right));
}
}
for (Double left : doubleLefts) {
for (Integer right : intRights) {
assertExecute(generateExpression("log(%s, %s)", left, right), DOUBLE, left == null || right == null ? null : MathFunctions.log(left, right));
}
}
for (Double left : doubleLefts) {
for (Double right : doubleRights) {
assertExecute(generateExpression("log(%s, %s)", left, right), DOUBLE, left == null || right == null ? null : MathFunctions.log(left, right));
}
}
for (String value : stringLefts) {
for (Integer start : intLefts) {
for (Integer length : intRights) {
String expected;
if (value == null || start == null || length == null) {
expected = null;
} else {
expected = StringFunctions.substr(utf8Slice(value), start, length).toStringUtf8();
}
VarcharType expectedType = value != null ? createVarcharType(value.length()) : VARCHAR;
assertExecute(generateExpression("substr(%s, %s, %s)", value, start, length), expectedType, expected);
}
}
}
Futures.allAsList(futures).get();
}
use of com.facebook.presto.spi.type.VarcharType in project presto by prestodb.
the class LiteralInterpreter method toExpression.
public static Expression toExpression(Object object, Type type) {
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, true);
}
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());
} else if (value.equals(Double.NEGATIVE_INFINITY)) {
return ArithmeticUnaryExpression.negative(new FunctionCall(QualifiedName.of("infinity"), ImmutableList.of()));
} else if (value.equals(Double.POSITIVE_INFINITY)) {
return new FunctionCall(QualifiedName.of("infinity"), ImmutableList.of());
} else {
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);
} else if (value.equals(Float.NEGATIVE_INFINITY)) {
return ArithmeticUnaryExpression.negative(new Cast(new FunctionCall(QualifiedName.of("infinity"), ImmutableList.of()), StandardTypes.REAL));
} else if (value.equals(Float.POSITIVE_INFINITY)) {
return new Cast(new FunctionCall(QualifiedName.of("infinity"), ImmutableList.of()), StandardTypes.REAL);
} else {
return new GenericLiteral("REAL", value.toString());
}
}
if (type instanceof VarcharType) {
if (object instanceof String) {
object = Slices.utf8Slice((String) object);
}
if (object instanceof Slice) {
Slice value = (Slice) object;
int length = SliceUtf8.countCodePoints(value);
if (length == ((VarcharType) type).getLength()) {
return new StringLiteral(value.toStringUtf8());
}
return new Cast(new StringLiteral(value.toStringUtf8()), type.getDisplayName(), false, true);
}
throw new IllegalArgumentException("object must be instance of Slice or String when type is VARCHAR");
}
if (type.equals(BOOLEAN)) {
return new BooleanLiteral(object.toString());
}
if (object instanceof Block) {
SliceOutput output = new DynamicSliceOutput(((Block) object).getSizeInBytes());
BlockSerdeUtil.writeBlock(output, (Block) object);
object = output.slice();
// This if condition will evaluate to true: object instanceof Slice && !type.equals(VARCHAR)
}
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())));
Signature signature = FunctionRegistry.getMagicLiteralFunctionSignature(type);
return new FunctionCall(QualifiedName.of(signature.getName()), ImmutableList.of(fromBase64));
}
Signature signature = FunctionRegistry.getMagicLiteralFunctionSignature(type);
Expression rawLiteral = toExpression(object, FunctionRegistry.typeForMagicLiteral(type));
return new FunctionCall(QualifiedName.of(signature.getName()), ImmutableList.of(rawLiteral));
}
use of com.facebook.presto.spi.type.VarcharType in project presto by prestodb.
the class Row method valueFromString.
/**
* Converts the given String into a Java object based on the given Presto type
*
* @param str String to convert
* @param type Presto Type
* @return Java object
* @throws PrestoException If the type is not supported by this function
*/
public static Object valueFromString(String str, Type type) {
if (str == null || str.isEmpty()) {
return null;
} else if (Types.isArrayType(type)) {
Type elementType = Types.getElementType(type);
ImmutableList.Builder<Object> listBuilder = ImmutableList.builder();
for (String element : Splitter.on(',').split(str)) {
listBuilder.add(valueFromString(element, elementType));
}
return AccumuloRowSerializer.getBlockFromArray(elementType, listBuilder.build());
} else if (Types.isMapType(type)) {
Type keyType = Types.getKeyType(type);
Type valueType = Types.getValueType(type);
ImmutableMap.Builder<Object, Object> mapBuilder = ImmutableMap.builder();
for (String element : Splitter.on(',').split(str)) {
ImmutableList.Builder<String> builder = ImmutableList.builder();
List<String> keyValue = builder.addAll(Splitter.on("->").split(element)).build();
checkArgument(keyValue.size() == 2, format("Map element %s has %d entries, not 2", element, keyValue.size()));
mapBuilder.put(valueFromString(keyValue.get(0), keyType), valueFromString(keyValue.get(1), valueType));
}
return AccumuloRowSerializer.getBlockFromMap(type, mapBuilder.build());
} else if (type.equals(BIGINT)) {
return Long.parseLong(str);
} else if (type.equals(BOOLEAN)) {
return Boolean.parseBoolean(str);
} else if (type.equals(DATE)) {
return new Date(TimeUnit.MILLISECONDS.toDays(DATE_PARSER.parseDateTime(str).getMillis()));
} else if (type.equals(DOUBLE)) {
return Double.parseDouble(str);
} else if (type.equals(INTEGER)) {
return Integer.parseInt(str);
} else if (type.equals(REAL)) {
return Float.parseFloat(str);
} else if (type.equals(SMALLINT)) {
return Short.parseShort(str);
} else if (type.equals(TIME)) {
return new Time(TIME_PARSER.parseDateTime(str).getMillis());
} else if (type.equals(TIMESTAMP)) {
return new Timestamp(TIMESTAMP_PARSER.parseDateTime(str).getMillis());
} else if (type.equals(TINYINT)) {
return Byte.valueOf(str);
} else if (type.equals(VARBINARY)) {
return str.getBytes(UTF_8);
} else if (type instanceof VarcharType) {
return str;
} else {
throw new PrestoException(NOT_SUPPORTED, "Unsupported type " + type);
}
}
use of com.facebook.presto.spi.type.VarcharType in project presto by prestodb.
the class Field method toString.
@Override
public String toString() {
if (value == null) {
return "null";
}
if (Types.isArrayType(type)) {
Type elementType = Types.getElementType(type);
StringBuilder builder = new StringBuilder("ARRAY [");
for (Object element : AccumuloRowSerializer.getArrayFromBlock(elementType, this.getArray())) {
if (Types.isArrayType(elementType)) {
Type elementElementType = Types.getElementType(elementType);
builder.append(new Field(AccumuloRowSerializer.getBlockFromArray(elementElementType, (List<?>) element), elementType)).append(',');
} else if (Types.isMapType(elementType)) {
builder.append(new Field(AccumuloRowSerializer.getBlockFromMap(elementType, (Map<?, ?>) element), elementType)).append(',');
} else {
builder.append(new Field(element, elementType)).append(',');
}
}
return builder.deleteCharAt(builder.length() - 1).append("]").toString();
}
if (Types.isMapType(type)) {
StringBuilder builder = new StringBuilder("MAP(");
StringBuilder keys = new StringBuilder("ARRAY [");
StringBuilder values = new StringBuilder("ARRAY [");
for (Entry<Object, Object> entry : AccumuloRowSerializer.getMapFromBlock(type, this.getMap()).entrySet()) {
Type keyType = Types.getKeyType(type);
if (Types.isArrayType(keyType)) {
keys.append(new Field(AccumuloRowSerializer.getBlockFromArray(Types.getElementType(keyType), (List<?>) entry.getKey()), keyType)).append(',');
} else if (Types.isMapType(keyType)) {
keys.append(new Field(AccumuloRowSerializer.getBlockFromMap(keyType, (Map<?, ?>) entry.getKey()), keyType)).append(',');
} else {
keys.append(new Field(entry.getKey(), keyType)).append(',');
}
Type valueType = Types.getValueType(type);
if (Types.isArrayType(valueType)) {
values.append(new Field(AccumuloRowSerializer.getBlockFromArray(Types.getElementType(valueType), (List<?>) entry.getValue()), valueType)).append(',');
} else if (Types.isMapType(valueType)) {
values.append(new Field(AccumuloRowSerializer.getBlockFromMap(valueType, (Map<?, ?>) entry.getValue()), valueType)).append(',');
} else {
values.append(new Field(entry.getValue(), valueType)).append(',');
}
}
keys.deleteCharAt(keys.length() - 1).append(']');
values.deleteCharAt(values.length() - 1).append(']');
return builder.append(keys).append(", ").append(values).append(")").toString();
}
// Validate the object is the given type
if (type.equals(BIGINT) || type.equals(BOOLEAN) || type.equals(DOUBLE) || type.equals(INTEGER) || type.equals(REAL) || type.equals(TINYINT) || type.equals(SMALLINT)) {
return value.toString();
} else if (type.equals(DATE)) {
return "DATE '" + value.toString() + "'";
} else if (type.equals(TIME)) {
return "TIME '" + value.toString() + "'";
} else if (type.equals(TIMESTAMP)) {
return "TIMESTAMP '" + value.toString() + "'";
} else if (type.equals(VARBINARY)) {
return "CAST('" + new String((byte[]) value, UTF_8).replaceAll("'", "''") + "' AS VARBINARY)";
} else if (type instanceof VarcharType) {
return "'" + value.toString().replaceAll("'", "''") + "'";
} else {
throw new PrestoException(NOT_SUPPORTED, "Unsupported PrestoType " + type);
}
}
Aggregations