Java Code Examples for io.prestosql.spi.type.VarbinaryType

The following examples show how to use io.prestosql.spi.type.VarbinaryType. These examples are extracted from open source projects. 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 Project: presto   Source File: TupleDomainOrcPredicate.java    License: Apache License 2.0 6 votes vote down vote up
@VisibleForTesting
public static boolean checkInBloomFilter(BloomFilter bloomFilter, Object predicateValue, Type sqlType)
{
    if (sqlType == TINYINT || sqlType == SMALLINT || sqlType == INTEGER || sqlType == BIGINT || sqlType == DATE || sqlType.equals(TIMESTAMP)) {
        return bloomFilter.testLong(((Number) predicateValue).longValue());
    }

    if (sqlType == DOUBLE) {
        return bloomFilter.testDouble((Double) predicateValue);
    }

    if (sqlType == REAL) {
        return bloomFilter.testFloat(intBitsToFloat(((Number) predicateValue).intValue()));
    }

    if (sqlType instanceof VarcharType || sqlType instanceof VarbinaryType) {
        return bloomFilter.testSlice(((Slice) predicateValue));
    }

    // todo support DECIMAL, and CHAR
    return true;
}
 
Example 2
Source Project: presto   Source File: MongoPageSource.java    License: Apache License 2.0 6 votes vote down vote up
private void writeSlice(BlockBuilder output, Type type, Object value)
{
    if (type instanceof VarcharType) {
        type.writeSlice(output, utf8Slice(toVarcharValue(value)));
    }
    else if (type instanceof CharType) {
        type.writeSlice(output, truncateToLengthAndTrimSpaces(utf8Slice((String) value), ((CharType) type)));
    }
    else if (type.equals(OBJECT_ID)) {
        type.writeSlice(output, wrappedBuffer(((ObjectId) value).toByteArray()));
    }
    else if (type instanceof VarbinaryType) {
        if (value instanceof Binary) {
            type.writeSlice(output, wrappedBuffer(((Binary) value).getData()));
        }
        else {
            output.appendNull();
        }
    }
    else if (type instanceof DecimalType) {
        type.writeSlice(output, encodeScaledValue(((Decimal128) value).bigDecimalValue(), ((DecimalType) type).getScale()));
    }
    else {
        throw new PrestoException(GENERIC_INTERNAL_ERROR, "Unhandled type for Slice: " + type.getTypeSignature());
    }
}
 
Example 3
Source Project: presto   Source File: BigQueryResultPageSource.java    License: Apache License 2.0 6 votes vote down vote up
private static void writeSlice(BlockBuilder output, Type type, Object value)
{
    if (type instanceof VarcharType) {
        type.writeSlice(output, utf8Slice(((Utf8) value).toString()));
    }
    else if (type instanceof DecimalType) {
        BigDecimal bdValue = DECIMAL_CONVERTER.convert(value);
        type.writeSlice(output, Decimals.encodeScaledValue(bdValue, NUMERIC_DATA_TYPE_SCALE));
    }
    else if (type instanceof VarbinaryType) {
        if (value instanceof ByteBuffer) {
            type.writeSlice(output, Slices.wrappedBuffer((ByteBuffer) value));
        }
        else {
            output.appendNull();
        }
    }
    else {
        throw new PrestoException(GENERIC_INTERNAL_ERROR, "Unhandled type for Slice: " + type.getTypeSignature());
    }
}
 
Example 4
Source Project: presto   Source File: PinotColumn.java    License: Apache License 2.0 6 votes vote down vote up
public static Type getPrestoTypeFromPinotType(DataType dataType)
{
    switch (dataType) {
        case BOOLEAN:
            return BooleanType.BOOLEAN;
        case FLOAT:
        case DOUBLE:
            return DoubleType.DOUBLE;
        case INT:
            return IntegerType.INTEGER;
        case LONG:
            return BigintType.BIGINT;
        case STRING:
            return VarcharType.VARCHAR;
        case BYTES:
            return VarbinaryType.VARBINARY;
        default:
            break;
    }
    throw new PinotException(PINOT_UNSUPPORTED_COLUMN_TYPE, Optional.empty(), "Not support type conversion for pinot data type: " + dataType);
}
 
Example 5
Source Project: presto   Source File: AvroColumnDecoder.java    License: Apache License 2.0 6 votes vote down vote up
private static Slice getSlice(Object value, Type type, String columnName)
{
    if (type instanceof VarcharType && (value instanceof CharSequence || value instanceof GenericEnumSymbol)) {
        return truncateToLength(utf8Slice(value.toString()), type);
    }

    if (type instanceof VarbinaryType) {
        if (value instanceof ByteBuffer) {
            return Slices.wrappedBuffer((ByteBuffer) value);
        }
        else if (value instanceof GenericFixed) {
            return Slices.wrappedBuffer(((GenericFixed) value).bytes());
        }
    }

    throw new PrestoException(DECODER_CONVERSION_NOT_SUPPORTED, format("cannot decode object of '%s' as '%s' for column '%s'", value.getClass(), type, columnName));
}
 
Example 6
Source Project: presto   Source File: TestAvroDecoder.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testSupportedDataTypeValidation()
{
    // supported types
    singleColumnDecoder(BigintType.BIGINT);
    singleColumnDecoder(VarbinaryType.VARBINARY);
    singleColumnDecoder(BooleanType.BOOLEAN);
    singleColumnDecoder(DoubleType.DOUBLE);
    singleColumnDecoder(createUnboundedVarcharType());
    singleColumnDecoder(createVarcharType(100));
    singleColumnDecoder(new ArrayType(BigintType.BIGINT));
    singleColumnDecoder(VARCHAR_MAP_TYPE);
    singleColumnDecoder(DOUBLE_MAP_TYPE);

    // some unsupported types
    assertUnsupportedColumnTypeException(() -> singleColumnDecoder(DecimalType.createDecimalType(10, 4)));
}
 
Example 7
Source Project: presto   Source File: TestRawDecoder.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testSupportedDataTypeValidation()
{
    // supported types
    singleColumnDecoder(BigintType.BIGINT, "0", "LONG");
    singleColumnDecoder(IntegerType.INTEGER, "0", "INT");
    singleColumnDecoder(SmallintType.SMALLINT, "0", "SHORT");
    singleColumnDecoder(TinyintType.TINYINT, "0", "BYTE");
    singleColumnDecoder(BooleanType.BOOLEAN, "0", "LONG");
    singleColumnDecoder(DoubleType.DOUBLE, "0", "DOUBLE");
    singleColumnDecoder(createUnboundedVarcharType(), "0", "BYTE");
    singleColumnDecoder(createVarcharType(100), "0", "BYTE");

    // some unsupported types
    assertUnsupportedColumnTypeException(() -> singleColumnDecoder(RealType.REAL, "0", "BYTE"));
    assertUnsupportedColumnTypeException(() -> singleColumnDecoder(DecimalType.createDecimalType(10, 4), "0", "BYTE"));
    assertUnsupportedColumnTypeException(() -> singleColumnDecoder(VarbinaryType.VARBINARY, "0", "BYTE"));
}
 
Example 8
Source Project: presto   Source File: TestCsvDecoder.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testSupportedDataTypeValidation()
{
    // supported types
    singleColumnDecoder(BigintType.BIGINT);
    singleColumnDecoder(IntegerType.INTEGER);
    singleColumnDecoder(SmallintType.SMALLINT);
    singleColumnDecoder(TinyintType.TINYINT);
    singleColumnDecoder(BooleanType.BOOLEAN);
    singleColumnDecoder(DoubleType.DOUBLE);
    singleColumnDecoder(createUnboundedVarcharType());
    singleColumnDecoder(createVarcharType(100));

    // some unsupported types
    assertUnsupportedColumnTypeException(() -> singleColumnDecoder(RealType.REAL));
    assertUnsupportedColumnTypeException(() -> singleColumnDecoder(DecimalType.createDecimalType(10, 4)));
    assertUnsupportedColumnTypeException(() -> singleColumnDecoder(VarbinaryType.VARBINARY));
}
 
Example 9
Source Project: presto   Source File: BlackHolePageSourceProvider.java    License: Apache License 2.0 5 votes vote down vote up
private static boolean isSupportedType(Type type)
{
    return isNumericType(type) ||
            type instanceof BooleanType ||
            type instanceof DateType ||
            type instanceof TimestampType ||
            type instanceof VarcharType ||
            type instanceof VarbinaryType;
}
 
Example 10
Source Project: presto   Source File: AccumuloRecordCursor.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Slice getSlice(int field)
{
    Type type = getType(field);
    if (type instanceof VarbinaryType) {
        return Slices.wrappedBuffer(serializer.getVarbinary(fieldToColumnName[field]));
    }
    if (type instanceof VarcharType) {
        return Slices.utf8Slice(serializer.getVarchar(fieldToColumnName[field]));
    }
    throw new PrestoException(NOT_SUPPORTED, "Unsupported type " + type);
}
 
Example 11
Source Project: presto   Source File: SliceColumnReader.java    License: Apache License 2.0 5 votes vote down vote up
public SliceColumnReader(Type type, OrcColumn column, AggregatedMemoryContext systemMemoryContext)
        throws OrcCorruptionException
{
    requireNonNull(type, "type is null");
    verifyStreamType(column, type, t -> t instanceof VarcharType || t instanceof CharType || t instanceof VarbinaryType);

    this.column = requireNonNull(column, "column is null");

    int maxCodePointCount = getMaxCodePointCount(type);
    boolean charType = isCharType(type);
    directReader = new SliceDirectColumnReader(column, maxCodePointCount, charType);
    dictionaryReader = new SliceDictionaryColumnReader(column, systemMemoryContext.newLocalMemoryContext(SliceColumnReader.class.getSimpleName()), maxCodePointCount, charType);
}
 
Example 12
Source Project: presto   Source File: OrcFileWriter.java    License: Apache License 2.0 5 votes vote down vote up
private static StorageType toStorageType(Type type)
{
    if (type instanceof DecimalType) {
        DecimalType decimalType = (DecimalType) type;
        return StorageType.decimal(decimalType.getPrecision(), decimalType.getScale());
    }
    Class<?> javaType = type.getJavaType();
    if (javaType == boolean.class) {
        return StorageType.BOOLEAN;
    }
    if (javaType == long.class) {
        return StorageType.LONG;
    }
    if (javaType == double.class) {
        return StorageType.DOUBLE;
    }
    if (javaType == Slice.class) {
        if (type instanceof VarcharType) {
            return StorageType.STRING;
        }
        if (type.equals(VarbinaryType.VARBINARY)) {
            return StorageType.BYTES;
        }
    }
    if (isArrayType(type)) {
        return arrayOf(toStorageType(type.getTypeParameters().get(0)));
    }
    if (isMapType(type)) {
        return mapOf(toStorageType(type.getTypeParameters().get(0)), toStorageType(type.getTypeParameters().get(1)));
    }
    throw new PrestoException(NOT_SUPPORTED, "No storage type for type: " + type);
}
 
Example 13
Source Project: presto   Source File: ExpressionConverter.java    License: Apache License 2.0 5 votes vote down vote up
private static Object getIcebergLiteralValue(Type type, Marker marker)
{
    if (type instanceof IntegerType) {
        return toIntExact((long) marker.getValue());
    }

    if (type instanceof RealType) {
        return intBitsToFloat(toIntExact((long) marker.getValue()));
    }

    // TODO: Remove this conversion once we move to next iceberg version
    if (type instanceof DateType) {
        return toIntExact(((Long) marker.getValue()));
    }

    if (type instanceof VarcharType) {
        return ((Slice) marker.getValue()).toStringUtf8();
    }

    if (type instanceof VarbinaryType) {
        return ByteBuffer.wrap(((Slice) marker.getValue()).getBytes());
    }

    if (type instanceof DecimalType) {
        DecimalType decimalType = (DecimalType) type;
        Object value = requireNonNull(marker.getValue(), "The value of the marker must be non-null");
        if (Decimals.isShortDecimal(decimalType)) {
            checkArgument(value instanceof Long, "A short decimal should be represented by a Long value but was %s", value.getClass().getName());
            return BigDecimal.valueOf((long) value).movePointLeft(decimalType.getScale());
        }
        checkArgument(value instanceof Slice, "A long decimal should be represented by a Slice value but was %s", value.getClass().getName());
        return new BigDecimal(Decimals.decodeUnscaledValue((Slice) value), decimalType.getScale());
    }

    return marker.getValue();
}
 
Example 14
Source Project: presto   Source File: IcebergPageSink.java    License: Apache License 2.0 5 votes vote down vote up
public static Object getIcebergValue(Block block, int position, Type type)
{
    if (block.isNull(position)) {
        return null;
    }
    if (type instanceof BigintType) {
        return type.getLong(block, position);
    }
    if (type instanceof IntegerType || type instanceof SmallintType || type instanceof TinyintType || type instanceof DateType) {
        return toIntExact(type.getLong(block, position));
    }
    if (type instanceof BooleanType) {
        return type.getBoolean(block, position);
    }
    if (type instanceof DecimalType) {
        return readBigDecimal((DecimalType) type, block, position);
    }
    if (type instanceof RealType) {
        return intBitsToFloat(toIntExact(type.getLong(block, position)));
    }
    if (type instanceof DoubleType) {
        return type.getDouble(block, position);
    }
    if (type instanceof TimestampType) {
        return MILLISECONDS.toMicros(type.getLong(block, position));
    }
    if (type instanceof TimestampWithTimeZoneType) {
        return MILLISECONDS.toMicros(unpackMillisUtc(type.getLong(block, position)));
    }
    if (type instanceof VarbinaryType) {
        return type.getSlice(block, position).getBytes();
    }
    if (type instanceof VarcharType) {
        return type.getSlice(block, position).toStringUtf8();
    }
    throw new UnsupportedOperationException("Type not supported as partition column: " + type.getDisplayName());
}
 
Example 15
Source Project: presto   Source File: CassandraType.java    License: Apache License 2.0 5 votes vote down vote up
public static CassandraType toCassandraType(Type type, ProtocolVersion protocolVersion)
{
    if (type.equals(BooleanType.BOOLEAN)) {
        return BOOLEAN;
    }
    if (type.equals(BigintType.BIGINT)) {
        return BIGINT;
    }
    if (type.equals(IntegerType.INTEGER)) {
        return INT;
    }
    if (type.equals(SmallintType.SMALLINT)) {
        return SMALLINT;
    }
    if (type.equals(TinyintType.TINYINT)) {
        return TINYINT;
    }
    if (type.equals(DoubleType.DOUBLE)) {
        return DOUBLE;
    }
    if (type.equals(RealType.REAL)) {
        return FLOAT;
    }
    if (isVarcharType(type)) {
        return TEXT;
    }
    if (type.equals(DateType.DATE)) {
        return protocolVersion.toInt() <= ProtocolVersion.V3.toInt() ? TEXT : DATE;
    }
    if (type.equals(VarbinaryType.VARBINARY)) {
        return BLOB;
    }
    if (type.equals(TimestampType.TIMESTAMP)) {
        return TIMESTAMP;
    }
    throw new IllegalArgumentException("unsupported type: " + type);
}
 
Example 16
Source Project: presto   Source File: KuduMetadata.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public ColumnMetadata getColumnMetadata(ConnectorSession session, ConnectorTableHandle tableHandle, ColumnHandle columnHandle)
{
    KuduColumnHandle kuduColumnHandle = (KuduColumnHandle) columnHandle;
    if (kuduColumnHandle.isVirtualRowId()) {
        return ColumnMetadata.builder()
                .setName(KuduColumnHandle.ROW_ID)
                .setType(VarbinaryType.VARBINARY)
                .setHidden(true)
                .build();
    }
    return kuduColumnHandle.getColumnMetadata();
}
 
Example 17
Source Project: presto   Source File: TypeHelper.java    License: Apache License 2.0 5 votes vote down vote up
private static Type fromKuduClientType(org.apache.kudu.Type ktype, ColumnTypeAttributes attributes)
{
    switch (ktype) {
        case STRING:
            return VarcharType.VARCHAR;
        case UNIXTIME_MICROS:
            return TimestampType.TIMESTAMP;
        case INT64:
            return BigintType.BIGINT;
        case INT32:
            return IntegerType.INTEGER;
        case INT16:
            return SmallintType.SMALLINT;
        case INT8:
            return TinyintType.TINYINT;
        case FLOAT:
            return RealType.REAL;
        case DOUBLE:
            return DoubleType.DOUBLE;
        case BOOL:
            return BooleanType.BOOLEAN;
        case BINARY:
            return VarbinaryType.VARBINARY;
        case DECIMAL:
            return DecimalType.createDecimalType(attributes.getPrecision(), attributes.getScale());
        default:
            throw new IllegalStateException("Kudu type not implemented for " + ktype);
    }
}
 
Example 18
Source Project: presto   Source File: TypeHelper.java    License: Apache License 2.0 5 votes vote down vote up
public static Object getObject(Type type, RowResult row, int field)
{
    if (row.isNull(field)) {
        return null;
    }
    if (type instanceof VarcharType) {
        return row.getString(field);
    }
    if (type.equals(TimestampType.TIMESTAMP)) {
        return row.getLong(field) / 1000;
    }
    if (type == BigintType.BIGINT) {
        return row.getLong(field);
    }
    if (type == IntegerType.INTEGER) {
        return row.getInt(field);
    }
    if (type == SmallintType.SMALLINT) {
        return row.getShort(field);
    }
    if (type == TinyintType.TINYINT) {
        return row.getByte(field);
    }
    if (type == DoubleType.DOUBLE) {
        return row.getDouble(field);
    }
    if (type == RealType.REAL) {
        return row.getFloat(field);
    }
    if (type == BooleanType.BOOLEAN) {
        return row.getBoolean(field);
    }
    if (type instanceof VarbinaryType) {
        return Slices.wrappedBuffer(row.getBinary(field));
    }
    if (type instanceof DecimalType) {
        return row.getDecimal(field);
    }
    throw new IllegalStateException("getObject not implemented for " + type);
}
 
Example 19
Source Project: presto   Source File: TypeHelper.java    License: Apache License 2.0 5 votes vote down vote up
public static Slice getSlice(Type type, RowResult row, int field)
{
    if (type instanceof VarcharType) {
        return Slices.utf8Slice(row.getString(field));
    }
    if (type instanceof VarbinaryType) {
        return Slices.wrappedBuffer(row.getBinary(field));
    }
    if (type instanceof DecimalType) {
        BigDecimal dec = row.getDecimal(field);
        return Decimals.encodeScaledValue(dec);
    }
    throw new IllegalStateException("getSlice not implemented for " + type);
}
 
Example 20
Source Project: presto   Source File: OracleDataTypes.java    License: Apache License 2.0 5 votes vote down vote up
public static DataType<byte[]> blobDataType()
{
    return dataType("blob", VarbinaryType.VARBINARY,
            // hextoraw('') is NULL, but you can create an empty BLOB directly
            bytes -> bytes.length == 0
                    ? "empty_blob()"
                    : format("hextoraw('%s')", base16().encode(bytes)));
}
 
Example 21
Source Project: presto   Source File: HiveWriteUtils.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void setField(Block block, int position)
{
    byte[] bytes = VarbinaryType.VARBINARY.getSlice(block, position).getBytes();
    value.set(bytes, 0, bytes.length);
    rowInspector.setStructFieldData(row, field, value);
}
 
Example 22
Source Project: presto   Source File: ParquetWriters.java    License: Apache License 2.0 5 votes vote down vote up
private static PrimitiveValueWriter getValueWriter(ValuesWriter valuesWriter, io.prestosql.spi.type.Type type, PrimitiveType parquetType)
{
    if (BOOLEAN.equals(type)) {
        return new BooleanValueWriter(valuesWriter, parquetType);
    }
    if (INTEGER.equals(type) || SMALLINT.equals(type) || TINYINT.equals(type)) {
        return new IntegerValueWriter(valuesWriter, type, parquetType);
    }
    if (type instanceof DecimalType) {
        return new DecimalValueWriter(valuesWriter, type, parquetType);
    }
    if (DATE.equals(type)) {
        return new DateValueWriter(valuesWriter, parquetType);
    }
    if (BIGINT.equals(type) || TIMESTAMP.equals(type)) {
        return new BigintValueWriter(valuesWriter, type, parquetType);
    }
    if (DOUBLE.equals(type)) {
        return new DoubleValueWriter(valuesWriter, parquetType);
    }
    if (RealType.REAL.equals(type)) {
        return new RealValueWriter(valuesWriter, parquetType);
    }
    if (type instanceof VarcharType || type instanceof CharType || type instanceof VarbinaryType) {
        return new CharValueWriter(valuesWriter, type, parquetType);
    }
    throw new PrestoException(NOT_SUPPORTED, format("Unsupported type in parquet writer: %s", type));
}
 
Example 23
Source Project: presto   Source File: PinotSegmentPageSource.java    License: Apache License 2.0 5 votes vote down vote up
Slice getSlice(int rowIndex, int columnIndex)
{
    Type prestoType = getType(columnIndex);
    if (prestoType instanceof VarcharType) {
        String field = currentDataTable.getDataTable().getString(rowIndex, columnIndex);
        return getUtf8Slice(field);
    }
    else if (prestoType instanceof VarbinaryType) {
        return Slices.wrappedBuffer(toBytes(currentDataTable.getDataTable().getString(rowIndex, columnIndex)));
    }
    return Slices.EMPTY_SLICE;
}
 
Example 24
Source Project: presto   Source File: AvroColumnDecoder.java    License: Apache License 2.0 5 votes vote down vote up
private static void serializePrimitive(BlockBuilder blockBuilder, Object value, Type type, String columnName)
{
    requireNonNull(blockBuilder, "parent blockBuilder is null");

    if (value == null) {
        blockBuilder.appendNull();
        return;
    }

    if (type instanceof BooleanType) {
        type.writeBoolean(blockBuilder, (Boolean) value);
        return;
    }

    if ((value instanceof Integer || value instanceof Long) && (type instanceof BigintType || type instanceof IntegerType || type instanceof SmallintType || type instanceof TinyintType)) {
        type.writeLong(blockBuilder, ((Number) value).longValue());
        return;
    }

    if (type instanceof DoubleType) {
        type.writeDouble(blockBuilder, (Double) value);
        return;
    }

    if (type instanceof RealType) {
        type.writeLong(blockBuilder, floatToIntBits((Float) value));
        return;
    }

    if (type instanceof VarcharType || type instanceof VarbinaryType) {
        type.writeSlice(blockBuilder, getSlice(value, type, columnName));
        return;
    }

    throw new PrestoException(DECODER_CONVERSION_NOT_SUPPORTED, format("cannot decode object of '%s' as '%s' for column '%s'", value.getClass(), type, columnName));
}
 
Example 25
Source Project: pulsar   Source File: PulsarRecordCursor.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Slice getSlice(int field) {
    checkFieldType(field, Slice.class);

    Object record = getRecord(field);
    Type type = getType(field);
    if (type == VarcharType.VARCHAR) {
        return Slices.utf8Slice(record.toString());
    } else if (type == VarbinaryType.VARBINARY) {
        return Slices.wrappedBuffer(toBytes(record));
    } else {
        throw new PrestoException(NOT_SUPPORTED, "Unsupported type " + type);
    }
}
 
Example 26
Source Project: pulsar   Source File: PulsarMetadata.java    License: Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
static Type convertPulsarType(SchemaType pulsarType) {
    switch (pulsarType) {
        case BOOLEAN:
            return BooleanType.BOOLEAN;
        case INT8:
            return TinyintType.TINYINT;
        case INT16:
            return SmallintType.SMALLINT;
        case INT32:
            return IntegerType.INTEGER;
        case INT64:
            return BigintType.BIGINT;
        case FLOAT:
            return RealType.REAL;
        case DOUBLE:
            return DoubleType.DOUBLE;
        case NONE:
        case BYTES:
            return VarbinaryType.VARBINARY;
        case STRING:
            return VarcharType.VARCHAR;
        case DATE:
            return DateType.DATE;
        case TIME:
            return TimeType.TIME;
        case TIMESTAMP:
            return TimestampType.TIMESTAMP;
        default:
            log.error("Cannot convert type: %s", pulsarType);
            return null;
    }
}
 
Example 27
Source Project: pulsar   Source File: PulsarMetadata.java    License: Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
static Type convertType(Schema.Type avroType, LogicalType logicalType) {
    switch (avroType) {
        case BOOLEAN:
            return BooleanType.BOOLEAN;
        case INT:
            if (logicalType == LogicalTypes.timeMillis()) {
                return TIME;
            } else if (logicalType == LogicalTypes.date()) {
                return DATE;
            }
            return IntegerType.INTEGER;
        case LONG:
            if (logicalType == LogicalTypes.timestampMillis()) {
                return TIMESTAMP;
            }
            return BigintType.BIGINT;
        case FLOAT:
            return RealType.REAL;
        case DOUBLE:
            return DoubleType.DOUBLE;
        case BYTES:
            return VarbinaryType.VARBINARY;
        case STRING:
            return VarcharType.VARCHAR;
        case ENUM:
            return VarcharType.VARCHAR;
        default:
            log.error("Cannot convert type: %s", avroType);
            return null;
    }
}
 
Example 28
Source Project: presto   Source File: TestingOrcPredicate.java    License: Apache License 2.0 4 votes vote down vote up
public static OrcPredicate createOrcPredicate(Type type, Iterable<?> values)
{
    List<Object> expectedValues = newArrayList(values);
    if (BOOLEAN.equals(type)) {
        return new BooleanOrcPredicate(expectedValues);
    }
    if (TINYINT.equals(type) || SMALLINT.equals(type) || INTEGER.equals(type) || BIGINT.equals(type)) {
        return new LongOrcPredicate(true,
                expectedValues.stream()
                        .map(value -> value == null ? null : ((Number) value).longValue())
                        .collect(toList()));
    }
    if (TIMESTAMP.equals(type)) {
        return new LongOrcPredicate(false,
                expectedValues.stream()
                        .map(value -> value == null ? null : ((SqlTimestamp) value).getMillisUtc())
                        .collect(toList()));
    }
    if (DATE.equals(type)) {
        return new DateOrcPredicate(
                expectedValues.stream()
                        .map(value -> value == null ? null : (long) ((SqlDate) value).getDays())
                        .collect(toList()));
    }
    if (REAL.equals(type) || DOUBLE.equals(type)) {
        return new DoubleOrcPredicate(
                expectedValues.stream()
                        .map(value -> value == null ? null : ((Number) value).doubleValue())
                        .collect(toList()));
    }
    if (type instanceof VarbinaryType) {
        // binary does not have stats
        return new BasicOrcPredicate<>(expectedValues, Object.class);
    }
    if (type instanceof VarcharType) {
        return new StringOrcPredicate(expectedValues);
    }
    if (type instanceof CharType) {
        return new CharOrcPredicate(expectedValues);
    }
    if (type instanceof DecimalType) {
        return new DecimalOrcPredicate(expectedValues);
    }

    if (type instanceof ArrayType || type instanceof MapType || type instanceof RowType) {
        return new BasicOrcPredicate<>(expectedValues, Object.class);
    }
    throw new IllegalArgumentException("Unsupported type " + type);
}
 
Example 29
Source Project: presto   Source File: OrcTester.java    License: Apache License 2.0 4 votes vote down vote up
private static ObjectInspector getJavaObjectInspector(Type type)
{
    if (type.equals(BOOLEAN)) {
        return javaBooleanObjectInspector;
    }
    if (type.equals(BIGINT)) {
        return javaLongObjectInspector;
    }
    if (type.equals(INTEGER)) {
        return javaIntObjectInspector;
    }
    if (type.equals(SMALLINT)) {
        return javaShortObjectInspector;
    }
    if (type.equals(TINYINT)) {
        return javaByteObjectInspector;
    }
    if (type.equals(REAL)) {
        return javaFloatObjectInspector;
    }
    if (type.equals(DOUBLE)) {
        return javaDoubleObjectInspector;
    }
    if (type instanceof VarcharType) {
        return javaStringObjectInspector;
    }
    if (type instanceof CharType) {
        int charLength = ((CharType) type).getLength();
        return new JavaHiveCharObjectInspector(getCharTypeInfo(charLength));
    }
    if (type instanceof VarbinaryType) {
        return javaByteArrayObjectInspector;
    }
    if (type.equals(DATE)) {
        return javaDateObjectInspector;
    }
    if (type.equals(TIMESTAMP)) {
        return javaTimestampObjectInspector;
    }
    if (type instanceof DecimalType) {
        DecimalType decimalType = (DecimalType) type;
        return getPrimitiveJavaObjectInspector(new DecimalTypeInfo(decimalType.getPrecision(), decimalType.getScale()));
    }
    if (type instanceof ArrayType) {
        return getStandardListObjectInspector(getJavaObjectInspector(type.getTypeParameters().get(0)));
    }
    if (type instanceof MapType) {
        ObjectInspector keyObjectInspector = getJavaObjectInspector(type.getTypeParameters().get(0));
        ObjectInspector valueObjectInspector = getJavaObjectInspector(type.getTypeParameters().get(1));
        return getStandardMapObjectInspector(keyObjectInspector, valueObjectInspector);
    }
    if (type instanceof RowType) {
        return getStandardStructObjectInspector(
                type.getTypeSignature().getParameters().stream()
                        .map(parameter -> parameter.getNamedTypeSignature().getName().get())
                        .collect(toList()),
                type.getTypeParameters().stream()
                        .map(OrcTester::getJavaObjectInspector)
                        .collect(toList()));
    }
    throw new IllegalArgumentException("unsupported type: " + type);
}
 
Example 30
Source Project: presto   Source File: DataType.java    License: Apache License 2.0 4 votes vote down vote up
public static DataType<byte[]> varbinaryDataType()
{
    return dataType("varbinary", VarbinaryType.VARBINARY, DataType::binaryLiteral, Function.identity());
}