Java Code Examples for com.facebook.presto.spi.type.Type#equals()

The following examples show how to use com.facebook.presto.spi.type.Type#equals() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EsTypeManager.java    From presto-connectors with Apache License 2.0 6 votes vote down vote up
public static Object getTypeValue(Type type, Object value)
{
    Object toEncode;
    if (Types.isArrayType(type)) {
        throw new UnsupportedOperationException("Unsupported type " + type);
    }
    else if (Types.isMapType(type)) {
        throw new UnsupportedOperationException("Unsupported type " + type);
    }
    else if (type.equals(VARBINARY)) {
        return ((Slice) value).getBytes();
    }
    else if (type instanceof VarcharType) {
        return ((Slice) value).toStringUtf8();
    }
    else {
        return value;
    }
}
 
Example 2
Source File: ElasticsearchPageSource.java    From presto-connectors with Apache License 2.0 6 votes vote down vote up
private void writeSlice(BlockBuilder output, Type type, Object value)
{
    String base = type.getTypeSignature().getBase();
    if (base.equals(StandardTypes.VARCHAR)) {
        type.writeSlice(output, utf8Slice(toVarcharValue(value)));
    }
    else if (type.equals(VARBINARY)) {
        if (value instanceof byte[]) {
            type.writeSlice(output, wrappedBuffer(((byte[]) value)));
        }
        else {
            output.appendNull();
        }
    }
    else {
        throw new PrestoException(GENERIC_INTERNAL_ERROR, "Unhandled type for Slice: " + type.getTypeSignature());
    }
}
 
Example 3
Source File: HbaseRowSerializerUtil.java    From presto-connectors with Apache License 2.0 6 votes vote down vote up
/**
 * @param type Presto type
 * @param block Block to decode
 * @param position Position in the block to get
 * @return Java object from the Block
 */
private static Object readObject(Type type, Block block, int position)
{
    if (Types.isArrayType(type)) {
        Type elementType = Types.getElementType(type);
        return getArrayFromBlock(elementType, block.getObject(position, Block.class));
    }
    else if (Types.isMapType(type)) {
        return getMapFromBlock(type, block.getObject(position, Block.class));
    }
    else {
        if (type.getJavaType() == Slice.class) {
            Slice slice = (Slice) TypeUtils.readNativeValue(type, block, position);
            return type.equals(VarcharType.VARCHAR) ? slice.toStringUtf8() : slice.getBytes();
        }

        return TypeUtils.readNativeValue(type, block, position);
    }
}
 
Example 4
Source File: KubeRecordCursor.java    From kubesql with Apache License 2.0 5 votes vote down vote up
private void checkFieldType(int field, Type... expected) {
    Type actual = getType(field);
    for (Type type : expected) {
        if (actual.equals(type)) {
            return;
        }
    }
    String expectedTypes = Joiner.on(", ").join(expected);
    throw new IllegalArgumentException(format("Expected field %s to be type %s but is %s", field, expectedTypes, actual));
}
 
Example 5
Source File: HbaseRecordCursor.java    From presto-connectors with Apache License 2.0 5 votes vote down vote up
@Override
public long getLong(int field)
{
    checkFieldType(field, BIGINT, DATE, INTEGER, REAL, SMALLINT, TIME, TIMESTAMP, TINYINT);
    Type type = getType(field);
    byte[] bytes = getValue(field);
    if (type.equals(BIGINT)) {
        return Bytes.toLong(bytes);
    }
    else if (type.equals(DATE)) {
        return Bytes.toLong(bytes);
    }
    else if (type.equals(INTEGER)) {
        return Bytes.toLong(bytes);
    }
    else if (type.equals(REAL)) {
        return Bytes.toLong(bytes);
    }
    else if (type.equals(SMALLINT)) {
        return Bytes.toLong(bytes);
    }
    else if (type.equals(TIME)) {
        return Bytes.toLong(bytes);
    }
    else if (type.equals(TIMESTAMP)) {
        return Bytes.toLong(bytes);
    }
    else if (type.equals(TINYINT)) {
        return Bytes.toLong(bytes);
    }
    else {
        throw new PrestoException(NOT_SUPPORTED, "Unsupported type " + getType(field));
    }
}
 
Example 6
Source File: HbaseRecordCursor.java    From presto-connectors with Apache License 2.0 5 votes vote down vote up
/**
 * Checks that the given field is one of the provided types.
 *
 * @param field Ordinal of the field
 * @param expected An array of expected types
 * @throws IllegalArgumentException If the given field does not match one of the types
 */
private void checkFieldType(int field, Type... expected)
{
    Type actual = getType(field);
    for (Type type : expected) {
        if (actual.equals(type)) {
            return;
        }
    }

    throw new IllegalArgumentException(format("Expected field %s to be a type of %s but is %s", field, StringUtils.join(expected, ","), actual));
}
 
Example 7
Source File: Elasticsearch2Client.java    From presto-connectors with Apache License 2.0 4 votes vote down vote up
private static XContentBuilder buildFieldType(XContentBuilder fieldBuilder, Type type)
        throws IOException
{
    final String dateTimeFormat = "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis";
    //Type mapping
    // see:https://www.elastic.co/guide/en/elasticsearch/reference/6.3/mapping-types.html
    if (type.equals(BooleanType.BOOLEAN)) {
        return fieldBuilder.field("type", "boolean");
    }
    if (type.equals(BigintType.BIGINT)) {
        return fieldBuilder.field("type", "long");
    }
    if (type.equals(IntegerType.INTEGER)) {
        return fieldBuilder.field("type", "integer");
    }
    if (type.equals(SmallintType.SMALLINT)) {
        return fieldBuilder.field("type", "short");
    }
    if (type.equals(TinyintType.TINYINT)) {
        return fieldBuilder.field("type", "byte");
    }
    if (type.equals(DoubleType.DOUBLE)) {
        return fieldBuilder.field("type", "double");
    }
    if (type.equals(DateType.DATE)) {
        return fieldBuilder.field("type", "date")
                .field("format", dateTimeFormat);
    }
    if (type.equals(TimeType.TIME)) {
        return fieldBuilder.field("type", "date")
                .field("format", dateTimeFormat);
    }
    if (type.equals(TimestampType.TIMESTAMP)) {
        return fieldBuilder.field("type", "date")
                .field("format", dateTimeFormat);
    }
    if (type.equals(TimestampWithTimeZoneType.TIMESTAMP_WITH_TIME_ZONE)) {
        //TODO: TIMESTAMP_WITH_TIME_ZONE
        return fieldBuilder.field("type", "date")
                .field("format", "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis");
    }
    if (type instanceof DecimalType) {
        return fieldBuilder.field("type", "double");
    }
    if (isVarcharType(type)) {
        //es 2.4.x
        // .field("index", "not_analyzed")
        return fieldBuilder.field("type", "string");
    }
    if (type.equals(VarbinaryType.VARBINARY)) {
        return fieldBuilder.field("type", "binary");
    }
    if (isArrayType(type)) {
        Type elementType = type.getTypeParameters().get(0);
        if (isArrayType(elementType) || isMapType(elementType) || isRowType(elementType)) {
            throw new PrestoException(NOT_SUPPORTED, "sorry unsupported type: " + type);
        }
        return buildFieldType(fieldBuilder, elementType);
    }
    if (isMapType(type)) {
        throw new PrestoException(NOT_SUPPORTED, "sorry unsupported type: " + type);
    }
    if (isRowType(type)) {
        throw new PrestoException(NOT_SUPPORTED, "sorry unsupported type: " + type);
    }

    throw new PrestoException(NOT_SUPPORTED, "unsupported type: " + type);
}
 
Example 8
Source File: Elasticsearch6Client.java    From presto-connectors with Apache License 2.0 4 votes vote down vote up
private static XContentBuilder buildFieldType(XContentBuilder fieldBuilder, Type type)
        throws IOException
{
    final String dateTimeFormat = "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis";
    //Type mapping
    // see:https://www.elastic.co/guide/en/elasticsearch/reference/6.3/mapping-types.html
    if (type.equals(BooleanType.BOOLEAN)) {
        return fieldBuilder.field("type", "boolean");
    }
    if (type.equals(BigintType.BIGINT)) {
        return fieldBuilder.field("type", "long");
    }
    if (type.equals(IntegerType.INTEGER)) {
        return fieldBuilder.field("type", "integer");
    }
    if (type.equals(SmallintType.SMALLINT)) {
        return fieldBuilder.field("type", "short");
    }
    if (type.equals(TinyintType.TINYINT)) {
        return fieldBuilder.field("type", "byte");
    }
    if (type.equals(DoubleType.DOUBLE)) {
        return fieldBuilder.field("type", "double");
    }
    if (type.equals(DateType.DATE)) {
        return fieldBuilder.field("type", "date")
                .field("format", dateTimeFormat);
    }
    if (type.equals(TimeType.TIME)) {
        return fieldBuilder.field("type", "date")
                .field("format", dateTimeFormat);
    }
    if (type.equals(TimestampType.TIMESTAMP)) {
        return fieldBuilder.field("type", "date")
                .field("format", dateTimeFormat);
    }
    if (type.equals(TimestampWithTimeZoneType.TIMESTAMP_WITH_TIME_ZONE)) {
        //TODO: TIMESTAMP_WITH_TIME_ZONE
        return fieldBuilder.field("type", "date")
                .field("format", "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis");
    }
    if (type instanceof DecimalType) {
        return fieldBuilder.field("type", "double");
    }
    if (isVarcharType(type)) {
        //TODO: text or keyword ?
        return fieldBuilder.field("type", "text");
    }
    if (type.equals(VarbinaryType.VARBINARY)) {
        return fieldBuilder.field("type", "binary");
    }
    if (isArrayType(type)) {
        Type elementType = type.getTypeParameters().get(0);
        if (isArrayType(elementType) || isMapType(elementType) || isRowType(elementType)) {
            throw new PrestoException(NOT_SUPPORTED, "sorry unsupported type: " + type);
        }
        return buildFieldType(fieldBuilder, elementType);
    }
    if (isMapType(type)) {
        throw new PrestoException(NOT_SUPPORTED, "sorry unsupported type: " + type);
    }
    if (isRowType(type)) {
        throw new PrestoException(NOT_SUPPORTED, "sorry unsupported type: " + type);
    }

    throw new PrestoException(NOT_SUPPORTED, "unsupported type: " + type);
}
 
Example 9
Source File: ElasticsearchPageSource.java    From presto-connectors with Apache License 2.0 4 votes vote down vote up
private void appendTo(Type type, Object value, BlockBuilder output)
{
    if (value == null) {
        output.appendNull();
        return;
    }
    Class<?> javaType = type.getJavaType();
    try {
        if (javaType == boolean.class) {
            type.writeBoolean(output, (Boolean) value);
        }
        else if (javaType == long.class) {
            if (type.equals(BIGINT)) {
                type.writeLong(output, ((Number) value).longValue());
            }
            else if (type.equals(INTEGER)) {
                type.writeLong(output, ((Number) value).intValue());
            }
            else if (type.equals(DATE)) {
                long millis = (Long) value;
                type.writeLong(output, TimeUnit.MILLISECONDS.toDays(millis));
            }
            else if (type.equals(TIME)) {
                type.writeLong(output, (Long) value);
            }
            else if (type.equals(TIMESTAMP)) {
                type.writeLong(output, (Long) value);
            }
            else {
                throw new PrestoException(GENERIC_INTERNAL_ERROR, "Unhandled type for " + javaType.getSimpleName() + ":" + type.getTypeSignature());
            }
        }
        else if (javaType == double.class) {
            type.writeDouble(output, ((Number) value).doubleValue());
        }
        else if (javaType == Slice.class) {
            writeSlice(output, type, value);
        }
        else if (javaType == Block.class) {
            writeBlock(output, type, value);
        }
        else {
            throw new PrestoException(GENERIC_INTERNAL_ERROR, "Unhandled type for " + javaType.getSimpleName() + ":" + type.getTypeSignature());
        }
    }
    catch (ClassCastException ignore) {
        // returns null instead of raising exception
        output.appendNull();
    }
}
 
Example 10
Source File: Elasticsearch5Client.java    From presto-connectors with Apache License 2.0 4 votes vote down vote up
private static XContentBuilder buildFieldType(XContentBuilder fieldBuilder, Type type)
        throws IOException
{
    final String dateTimeFormat = "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis";
    //Type mapping
    // see:https://www.elastic.co/guide/en/elasticsearch/reference/6.3/mapping-types.html
    if (type.equals(BooleanType.BOOLEAN)) {
        return fieldBuilder.field("type", "boolean");
    }
    if (type.equals(BigintType.BIGINT)) {
        return fieldBuilder.field("type", "long");
    }
    if (type.equals(IntegerType.INTEGER)) {
        return fieldBuilder.field("type", "integer");
    }
    if (type.equals(SmallintType.SMALLINT)) {
        return fieldBuilder.field("type", "short");
    }
    if (type.equals(TinyintType.TINYINT)) {
        return fieldBuilder.field("type", "byte");
    }
    if (type.equals(DoubleType.DOUBLE)) {
        return fieldBuilder.field("type", "double");
    }
    if (type.equals(DateType.DATE)) {
        return fieldBuilder.field("type", "date")
                .field("format", dateTimeFormat);
    }
    if (type.equals(TimeType.TIME)) {
        return fieldBuilder.field("type", "date")
                .field("format", dateTimeFormat);
    }
    if (type.equals(TimestampType.TIMESTAMP)) {
        return fieldBuilder.field("type", "date")
                .field("format", dateTimeFormat);
    }
    if (type.equals(TimestampWithTimeZoneType.TIMESTAMP_WITH_TIME_ZONE)) {
        //TODO: TIMESTAMP_WITH_TIME_ZONE
        return fieldBuilder.field("type", "date")
                .field("format", "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis");
    }
    if (type instanceof DecimalType) {
        return fieldBuilder.field("type", "double");
    }
    if (isVarcharType(type)) {
        //TODO: text or keyword ?
        return fieldBuilder.field("type", "text");
    }
    if (type.equals(VarbinaryType.VARBINARY)) {
        return fieldBuilder.field("type", "binary");
    }
    if (isArrayType(type)) {
        Type elementType = type.getTypeParameters().get(0);
        if (isArrayType(elementType) || isMapType(elementType) || isRowType(elementType)) {
            throw new PrestoException(NOT_SUPPORTED, "sorry unsupported type: " + type);
        }
        return buildFieldType(fieldBuilder, elementType);
    }
    if (isMapType(type)) {
        throw new PrestoException(NOT_SUPPORTED, "sorry unsupported type: " + type);
    }
    if (isRowType(type)) {
        throw new PrestoException(NOT_SUPPORTED, "sorry unsupported type: " + type);
    }

    throw new PrestoException(NOT_SUPPORTED, "unsupported type: " + type);
}