Search in sources :

Example 11 with VarcharType

use of com.facebook.presto.spi.type.VarcharType in project presto by prestodb.

the class HiveWriteUtils method getField.

public static Object getField(Type type, Block block, int position) {
    if (block.isNull(position)) {
        return null;
    }
    if (BooleanType.BOOLEAN.equals(type)) {
        return type.getBoolean(block, position);
    }
    if (BigintType.BIGINT.equals(type)) {
        return type.getLong(block, position);
    }
    if (IntegerType.INTEGER.equals(type)) {
        return (int) type.getLong(block, position);
    }
    if (SmallintType.SMALLINT.equals(type)) {
        return (short) type.getLong(block, position);
    }
    if (TinyintType.TINYINT.equals(type)) {
        return (byte) type.getLong(block, position);
    }
    if (RealType.REAL.equals(type)) {
        return intBitsToFloat((int) type.getLong(block, position));
    }
    if (DoubleType.DOUBLE.equals(type)) {
        return type.getDouble(block, position);
    }
    if (type instanceof VarcharType) {
        return new Text(type.getSlice(block, position).getBytes());
    }
    if (type instanceof CharType) {
        CharType charType = (CharType) type;
        return new Text(padEnd(type.getSlice(block, position).toStringUtf8(), charType.getLength(), ' '));
    }
    if (VarbinaryType.VARBINARY.equals(type)) {
        return type.getSlice(block, position).getBytes();
    }
    if (DateType.DATE.equals(type)) {
        long days = type.getLong(block, position);
        return new Date(UTC.getMillisKeepLocal(DateTimeZone.getDefault(), TimeUnit.DAYS.toMillis(days)));
    }
    if (TimestampType.TIMESTAMP.equals(type)) {
        long millisUtc = type.getLong(block, position);
        return new Timestamp(millisUtc);
    }
    if (type instanceof DecimalType) {
        DecimalType decimalType = (DecimalType) type;
        return getHiveDecimal(decimalType, block, position);
    }
    if (isArrayType(type)) {
        Type elementType = type.getTypeParameters().get(0);
        Block arrayBlock = block.getObject(position, Block.class);
        List<Object> list = new ArrayList<>(arrayBlock.getPositionCount());
        for (int i = 0; i < arrayBlock.getPositionCount(); i++) {
            Object element = getField(elementType, arrayBlock, i);
            list.add(element);
        }
        return Collections.unmodifiableList(list);
    }
    if (isMapType(type)) {
        Type keyType = type.getTypeParameters().get(0);
        Type valueType = type.getTypeParameters().get(1);
        Block mapBlock = block.getObject(position, Block.class);
        Map<Object, Object> map = new HashMap<>();
        for (int i = 0; i < mapBlock.getPositionCount(); i += 2) {
            Object key = getField(keyType, mapBlock, i);
            Object value = getField(valueType, mapBlock, i + 1);
            map.put(key, value);
        }
        return Collections.unmodifiableMap(map);
    }
    if (isRowType(type)) {
        Block rowBlock = block.getObject(position, Block.class);
        List<Type> fieldTypes = type.getTypeParameters();
        checkCondition(fieldTypes.size() == rowBlock.getPositionCount(), StandardErrorCode.GENERIC_INTERNAL_ERROR, "Expected row value field count does not match type field count");
        List<Object> row = new ArrayList<>(rowBlock.getPositionCount());
        for (int i = 0; i < rowBlock.getPositionCount(); i++) {
            Object element = getField(fieldTypes.get(i), rowBlock, i);
            row.add(element);
        }
        return Collections.unmodifiableList(row);
    }
    throw new PrestoException(NOT_SUPPORTED, "unsupported type: " + type);
}
Also used : VarcharType(com.facebook.presto.spi.type.VarcharType) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Text(org.apache.hadoop.io.Text) PrestoException(com.facebook.presto.spi.PrestoException) Timestamp(java.sql.Timestamp) Date(java.sql.Date) IntegerType(com.facebook.presto.spi.type.IntegerType) DoubleType(com.facebook.presto.spi.type.DoubleType) VarbinaryType(com.facebook.presto.spi.type.VarbinaryType) HiveUtil.isMapType(com.facebook.presto.hive.HiveUtil.isMapType) HiveUtil.isRowType(com.facebook.presto.hive.HiveUtil.isRowType) Type(com.facebook.presto.spi.type.Type) BigintType(com.facebook.presto.spi.type.BigintType) BooleanType(com.facebook.presto.spi.type.BooleanType) RealType(com.facebook.presto.spi.type.RealType) VarcharType(com.facebook.presto.spi.type.VarcharType) DateType(com.facebook.presto.spi.type.DateType) TinyintType(com.facebook.presto.spi.type.TinyintType) DecimalType(com.facebook.presto.spi.type.DecimalType) TimestampType(com.facebook.presto.spi.type.TimestampType) SmallintType(com.facebook.presto.spi.type.SmallintType) HiveUtil.isArrayType(com.facebook.presto.hive.HiveUtil.isArrayType) CharType(com.facebook.presto.spi.type.CharType) Chars.isCharType(com.facebook.presto.spi.type.Chars.isCharType) DecimalType(com.facebook.presto.spi.type.DecimalType) Block(com.facebook.presto.spi.block.Block) CharType(com.facebook.presto.spi.type.CharType) Chars.isCharType(com.facebook.presto.spi.type.Chars.isCharType)

Example 12 with VarcharType

use of com.facebook.presto.spi.type.VarcharType in project presto by prestodb.

the class HiveWriteUtils method getRowColumnInspector.

public static ObjectInspector getRowColumnInspector(Type type) {
    if (type.equals(BooleanType.BOOLEAN)) {
        return writableBooleanObjectInspector;
    }
    if (type.equals(BigintType.BIGINT)) {
        return writableLongObjectInspector;
    }
    if (type.equals(IntegerType.INTEGER)) {
        return writableIntObjectInspector;
    }
    if (type.equals(SmallintType.SMALLINT)) {
        return writableShortObjectInspector;
    }
    if (type.equals(TinyintType.TINYINT)) {
        return writableByteObjectInspector;
    }
    if (type.equals(RealType.REAL)) {
        return writableFloatObjectInspector;
    }
    if (type.equals(DoubleType.DOUBLE)) {
        return writableDoubleObjectInspector;
    }
    if (type instanceof VarcharType) {
        VarcharType varcharType = (VarcharType) type;
        int varcharLength = varcharType.getLength();
        // VARCHAR columns with the length less than or equal to 65535 are supported natively by Hive
        if (varcharLength <= HiveVarchar.MAX_VARCHAR_LENGTH) {
            return getPrimitiveWritableObjectInspector(getVarcharTypeInfo(varcharLength));
        } else // Values for such columns must be stored as STRING in Hive
        if (varcharLength == VarcharType.UNBOUNDED_LENGTH) {
            return writableStringObjectInspector;
        }
    }
    if (isCharType(type)) {
        CharType charType = (CharType) type;
        int charLength = charType.getLength();
        return getPrimitiveWritableObjectInspector(getCharTypeInfo(charLength));
    }
    if (type.equals(VarbinaryType.VARBINARY)) {
        return writableBinaryObjectInspector;
    }
    if (type.equals(DateType.DATE)) {
        return writableDateObjectInspector;
    }
    if (type.equals(TimestampType.TIMESTAMP)) {
        return writableTimestampObjectInspector;
    }
    if (type instanceof DecimalType) {
        DecimalType decimalType = (DecimalType) type;
        return getPrimitiveWritableObjectInspector(new DecimalTypeInfo(decimalType.getPrecision(), decimalType.getScale()));
    }
    if (isArrayType(type) || isMapType(type) || isRowType(type)) {
        return getJavaObjectInspector(type);
    }
    throw new IllegalArgumentException("unsupported type: " + type);
}
Also used : DecimalTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo) VarcharType(com.facebook.presto.spi.type.VarcharType) DecimalType(com.facebook.presto.spi.type.DecimalType) CharType(com.facebook.presto.spi.type.CharType) Chars.isCharType(com.facebook.presto.spi.type.Chars.isCharType)

Example 13 with VarcharType

use of com.facebook.presto.spi.type.VarcharType in project presto by prestodb.

the class HiveTypeTranslator method translate.

@Override
public TypeInfo translate(Type type) {
    if (BOOLEAN.equals(type)) {
        return HIVE_BOOLEAN.getTypeInfo();
    }
    if (BIGINT.equals(type)) {
        return HIVE_LONG.getTypeInfo();
    }
    if (INTEGER.equals(type)) {
        return HIVE_INT.getTypeInfo();
    }
    if (SMALLINT.equals(type)) {
        return HIVE_SHORT.getTypeInfo();
    }
    if (TINYINT.equals(type)) {
        return HIVE_BYTE.getTypeInfo();
    }
    if (REAL.equals(type)) {
        return HIVE_FLOAT.getTypeInfo();
    }
    if (DOUBLE.equals(type)) {
        return HIVE_DOUBLE.getTypeInfo();
    }
    if (type instanceof VarcharType) {
        VarcharType varcharType = (VarcharType) type;
        int varcharLength = varcharType.getLength();
        if (varcharLength <= HiveVarchar.MAX_VARCHAR_LENGTH) {
            return getVarcharTypeInfo(varcharLength);
        } else if (varcharLength == VarcharType.UNBOUNDED_LENGTH) {
            return HIVE_STRING.getTypeInfo();
        } else {
            throw new PrestoException(NOT_SUPPORTED, format("Unsupported Hive type: %s. Supported VARCHAR types: VARCHAR(<=%d), VARCHAR.", type, HiveVarchar.MAX_VARCHAR_LENGTH));
        }
    }
    if (type instanceof CharType) {
        CharType charType = (CharType) type;
        int charLength = charType.getLength();
        if (charLength <= HiveChar.MAX_CHAR_LENGTH) {
            return getCharTypeInfo(charLength);
        }
        throw new PrestoException(NOT_SUPPORTED, format("Unsupported Hive type: %s. Supported CHAR types: CHAR(<=%d).", type, HiveChar.MAX_CHAR_LENGTH));
    }
    if (VARBINARY.equals(type)) {
        return HIVE_BINARY.getTypeInfo();
    }
    if (DATE.equals(type)) {
        return HIVE_DATE.getTypeInfo();
    }
    if (TIMESTAMP.equals(type)) {
        return HIVE_TIMESTAMP.getTypeInfo();
    }
    if (type instanceof DecimalType) {
        DecimalType decimalType = (DecimalType) type;
        return new DecimalTypeInfo(decimalType.getPrecision(), decimalType.getScale());
    }
    if (isArrayType(type)) {
        TypeInfo elementType = translate(type.getTypeParameters().get(0));
        return getListTypeInfo(elementType);
    }
    if (isMapType(type)) {
        TypeInfo keyType = translate(type.getTypeParameters().get(0));
        TypeInfo valueType = translate(type.getTypeParameters().get(1));
        return getMapTypeInfo(keyType, valueType);
    }
    if (isRowType(type)) {
        ImmutableList.Builder<String> fieldNames = ImmutableList.builder();
        for (TypeSignatureParameter parameter : type.getTypeSignature().getParameters()) {
            if (!parameter.isNamedTypeSignature()) {
                throw new IllegalArgumentException(format("Expected all parameters to be named type, but got %s", parameter));
            }
            NamedTypeSignature namedTypeSignature = parameter.getNamedTypeSignature();
            fieldNames.add(namedTypeSignature.getName());
        }
        return getStructTypeInfo(fieldNames.build(), type.getTypeParameters().stream().map(this::translate).collect(toList()));
    }
    throw new PrestoException(NOT_SUPPORTED, format("Unsupported Hive type: %s", type));
}
Also used : VarcharType(com.facebook.presto.spi.type.VarcharType) ImmutableList(com.google.common.collect.ImmutableList) NamedTypeSignature(com.facebook.presto.spi.type.NamedTypeSignature) PrestoException(com.facebook.presto.spi.PrestoException) TypeInfoFactory.getCharTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.getCharTypeInfo) TypeInfoFactory.getStructTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.getStructTypeInfo) TypeInfoFactory.getVarcharTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.getVarcharTypeInfo) TypeInfoFactory.getMapTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.getMapTypeInfo) DecimalTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo) TypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfo) TypeInfoFactory.getListTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.getListTypeInfo) DecimalTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo) TypeSignatureParameter(com.facebook.presto.spi.type.TypeSignatureParameter) DecimalType(com.facebook.presto.spi.type.DecimalType) CharType(com.facebook.presto.spi.type.CharType)

Example 14 with VarcharType

use of com.facebook.presto.spi.type.VarcharType in project presto by prestodb.

the class QueryBuilder method buildSql.

public PreparedStatement buildSql(JdbcClient client, Connection connection, String catalog, String schema, String table, List<JdbcColumnHandle> columns, TupleDomain<ColumnHandle> tupleDomain) throws SQLException {
    StringBuilder sql = new StringBuilder();
    String columnNames = columns.stream().map(JdbcColumnHandle::getColumnName).map(this::quote).collect(joining(", "));
    sql.append("SELECT ");
    sql.append(columnNames);
    if (columns.isEmpty()) {
        sql.append("null");
    }
    sql.append(" FROM ");
    if (!isNullOrEmpty(catalog)) {
        sql.append(quote(catalog)).append('.');
    }
    if (!isNullOrEmpty(schema)) {
        sql.append(quote(schema)).append('.');
    }
    sql.append(quote(table));
    List<TypeAndValue> accumulator = new ArrayList<>();
    List<String> clauses = toConjuncts(columns, tupleDomain, accumulator);
    if (!clauses.isEmpty()) {
        sql.append(" WHERE ").append(Joiner.on(" AND ").join(clauses));
    }
    PreparedStatement statement = client.getPreparedStatement(connection, sql.toString());
    for (int i = 0; i < accumulator.size(); i++) {
        TypeAndValue typeAndValue = accumulator.get(i);
        if (typeAndValue.getType().equals(BigintType.BIGINT)) {
            statement.setLong(i + 1, (long) typeAndValue.getValue());
        } else if (typeAndValue.getType().equals(IntegerType.INTEGER)) {
            statement.setInt(i + 1, ((Number) typeAndValue.getValue()).intValue());
        } else if (typeAndValue.getType().equals(SmallintType.SMALLINT)) {
            statement.setShort(i + 1, ((Number) typeAndValue.getValue()).shortValue());
        } else if (typeAndValue.getType().equals(TinyintType.TINYINT)) {
            statement.setByte(i + 1, ((Number) typeAndValue.getValue()).byteValue());
        } else if (typeAndValue.getType().equals(DoubleType.DOUBLE)) {
            statement.setDouble(i + 1, (double) typeAndValue.getValue());
        } else if (typeAndValue.getType().equals(RealType.REAL)) {
            statement.setFloat(i + 1, intBitsToFloat(((Number) typeAndValue.getValue()).intValue()));
        } else if (typeAndValue.getType().equals(BooleanType.BOOLEAN)) {
            statement.setBoolean(i + 1, (boolean) typeAndValue.getValue());
        } else if (typeAndValue.getType().equals(DateType.DATE)) {
            long millis = DAYS.toMillis((long) typeAndValue.getValue());
            statement.setDate(i + 1, new Date(UTC.getMillisKeepLocal(DateTimeZone.getDefault(), millis)));
        } else if (typeAndValue.getType().equals(TimeType.TIME)) {
            statement.setTime(i + 1, new Time((long) typeAndValue.getValue()));
        } else if (typeAndValue.getType().equals(TimeWithTimeZoneType.TIME_WITH_TIME_ZONE)) {
            statement.setTime(i + 1, new Time(unpackMillisUtc((long) typeAndValue.getValue())));
        } else if (typeAndValue.getType().equals(TimestampType.TIMESTAMP)) {
            statement.setTimestamp(i + 1, new Timestamp((long) typeAndValue.getValue()));
        } else if (typeAndValue.getType().equals(TimestampWithTimeZoneType.TIMESTAMP_WITH_TIME_ZONE)) {
            statement.setTimestamp(i + 1, new Timestamp(unpackMillisUtc((long) typeAndValue.getValue())));
        } else if (typeAndValue.getType() instanceof VarcharType) {
            statement.setString(i + 1, ((Slice) typeAndValue.getValue()).toStringUtf8());
        } else {
            throw new UnsupportedOperationException("Can't handle type: " + typeAndValue.getType());
        }
    }
    return statement;
}
Also used : VarcharType(com.facebook.presto.spi.type.VarcharType) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) Time(java.sql.Time) Timestamp(java.sql.Timestamp) Date(java.sql.Date)

Example 15 with VarcharType

use of com.facebook.presto.spi.type.VarcharType in project presto by prestodb.

the class SequencePageBuilder method createSequencePage.

public static Page createSequencePage(List<? extends Type> types, int length, int... initialValues) {
    Block[] blocks = new Block[initialValues.length];
    for (int i = 0; i < blocks.length; i++) {
        Type type = types.get(i);
        int initialValue = initialValues[i];
        if (type.equals(BIGINT)) {
            blocks[i] = BlockAssertions.createLongSequenceBlock(initialValue, initialValue + length);
        } else if (type.equals(REAL)) {
            blocks[i] = BlockAssertions.createSequenceBlockOfReal(initialValue, initialValue + length);
        } else if (type.equals(DOUBLE)) {
            blocks[i] = BlockAssertions.createDoubleSequenceBlock(initialValue, initialValue + length);
        } else if (type instanceof VarcharType) {
            blocks[i] = BlockAssertions.createStringSequenceBlock(initialValue, initialValue + length);
        } else if (type.equals(BOOLEAN)) {
            blocks[i] = BlockAssertions.createBooleanSequenceBlock(initialValue, initialValue + length);
        } else if (type.equals(DATE)) {
            blocks[i] = BlockAssertions.createDateSequenceBlock(initialValue, initialValue + length);
        } else if (type.equals(TIMESTAMP)) {
            blocks[i] = BlockAssertions.createTimestampSequenceBlock(initialValue, initialValue + length);
        } else if (isShortDecimal(type)) {
            blocks[i] = BlockAssertions.createShortDecimalSequenceBlock(initialValue, initialValue + length, (DecimalType) type);
        } else if (isLongDecimal(type)) {
            blocks[i] = BlockAssertions.createLongDecimalSequenceBlock(initialValue, initialValue + length, (DecimalType) type);
        } else {
            throw new IllegalStateException("Unsupported type " + type);
        }
    }
    return new Page(blocks);
}
Also used : DecimalType(com.facebook.presto.spi.type.DecimalType) Type(com.facebook.presto.spi.type.Type) VarcharType(com.facebook.presto.spi.type.VarcharType) VarcharType(com.facebook.presto.spi.type.VarcharType) Block(com.facebook.presto.spi.block.Block) DecimalType(com.facebook.presto.spi.type.DecimalType) Page(com.facebook.presto.spi.Page)

Aggregations

VarcharType (com.facebook.presto.spi.type.VarcharType)25 Type (com.facebook.presto.spi.type.Type)14 DecimalType (com.facebook.presto.spi.type.DecimalType)10 PrestoException (com.facebook.presto.spi.PrestoException)9 Block (com.facebook.presto.spi.block.Block)8 CharType (com.facebook.presto.spi.type.CharType)7 ArrayList (java.util.ArrayList)7 VarcharType.createUnboundedVarcharType (com.facebook.presto.spi.type.VarcharType.createUnboundedVarcharType)6 Chars.isCharType (com.facebook.presto.spi.type.Chars.isCharType)5 Date (java.sql.Date)5 Timestamp (java.sql.Timestamp)5 BlockBuilderStatus (com.facebook.presto.spi.block.BlockBuilderStatus)4 ImmutableList (com.google.common.collect.ImmutableList)4 Slice (io.airlift.slice.Slice)4 HashMap (java.util.HashMap)4 List (java.util.List)4 BlockBuilder (com.facebook.presto.spi.block.BlockBuilder)3 ImmutableMap (com.google.common.collect.ImmutableMap)3 Time (java.sql.Time)3 DecimalTypeInfo (org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo)3