Search in sources :

Example 31 with VarcharType

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

the class MaterializedResult method writeValue.

private static void writeValue(Type type, BlockBuilder blockBuilder, Object value) {
    if (value == null) {
        blockBuilder.appendNull();
    } else if (BIGINT.equals(type)) {
        type.writeLong(blockBuilder, ((Number) value).longValue());
    } else if (INTEGER.equals(type)) {
        type.writeLong(blockBuilder, ((Number) value).intValue());
    } else if (SMALLINT.equals(type)) {
        type.writeLong(blockBuilder, ((Number) value).shortValue());
    } else if (TINYINT.equals(type)) {
        type.writeLong(blockBuilder, ((Number) value).byteValue());
    } else if (REAL.equals(type)) {
        type.writeLong(blockBuilder, (long) floatToRawIntBits(((Number) value).floatValue()));
    } else if (DOUBLE.equals(type)) {
        type.writeDouble(blockBuilder, ((Number) value).doubleValue());
    } else if (BOOLEAN.equals(type)) {
        type.writeBoolean(blockBuilder, (Boolean) value);
    } else if (type instanceof VarcharType) {
        type.writeSlice(blockBuilder, Slices.utf8Slice((String) value));
    } else if (type instanceof CharType) {
        type.writeSlice(blockBuilder, Slices.utf8Slice((String) value));
    } else if (VARBINARY.equals(type)) {
        type.writeSlice(blockBuilder, Slices.wrappedBuffer((byte[]) value));
    } else if (DATE.equals(type)) {
        int days = ((SqlDate) value).getDays();
        type.writeLong(blockBuilder, days);
    } else if (TIME.equals(type)) {
        SqlTime time = (SqlTime) value;
        if (time.isLegacyTimestamp()) {
            type.writeLong(blockBuilder, time.getMillisUtc());
        } else {
            type.writeLong(blockBuilder, time.getMillis());
        }
    } else if (TIME_WITH_TIME_ZONE.equals(type)) {
        long millisUtc = ((SqlTimeWithTimeZone) value).getMillisUtc();
        TimeZoneKey timeZoneKey = ((SqlTimeWithTimeZone) value).getTimeZoneKey();
        type.writeLong(blockBuilder, packDateTimeWithZone(millisUtc, timeZoneKey));
    } else if (TIMESTAMP.equals(type)) {
        long millisUtc = ((SqlTimestamp) value).getMillisUtc();
        type.writeLong(blockBuilder, millisUtc);
    } else if (TIMESTAMP_WITH_TIME_ZONE.equals(type)) {
        long millisUtc = ((SqlTimestampWithTimeZone) value).getMillisUtc();
        TimeZoneKey timeZoneKey = ((SqlTimestampWithTimeZone) value).getTimeZoneKey();
        type.writeLong(blockBuilder, packDateTimeWithZone(millisUtc, timeZoneKey));
    } else if (ARRAY.equals(type.getTypeSignature().getBase())) {
        List<Object> list = (List<Object>) value;
        Type elementType = ((ArrayType) type).getElementType();
        BlockBuilder arrayBlockBuilder = blockBuilder.beginBlockEntry();
        for (Object element : list) {
            writeValue(elementType, arrayBlockBuilder, element);
        }
        blockBuilder.closeEntry();
    } else if (MAP.equals(type.getTypeSignature().getBase())) {
        Map<Object, Object> map = (Map<Object, Object>) value;
        Type keyType = ((MapType) type).getKeyType();
        Type valueType = ((MapType) type).getValueType();
        BlockBuilder mapBlockBuilder = blockBuilder.beginBlockEntry();
        for (Entry<Object, Object> entry : map.entrySet()) {
            writeValue(keyType, mapBlockBuilder, entry.getKey());
            writeValue(valueType, mapBlockBuilder, entry.getValue());
        }
        blockBuilder.closeEntry();
    } else if (type instanceof RowType) {
        List<Object> row = (List<Object>) value;
        List<Type> fieldTypes = type.getTypeParameters();
        BlockBuilder rowBlockBuilder = blockBuilder.beginBlockEntry();
        for (int field = 0; field < row.size(); field++) {
            writeValue(fieldTypes.get(field), rowBlockBuilder, row.get(field));
        }
        blockBuilder.closeEntry();
    } else {
        throw new IllegalArgumentException("Unsupported type " + type);
    }
}
Also used : VarcharType(com.facebook.presto.common.type.VarcharType) SqlTime(com.facebook.presto.common.type.SqlTime) RowType(com.facebook.presto.common.type.RowType) SqlTimestamp(com.facebook.presto.common.type.SqlTimestamp) ArrayType(com.facebook.presto.common.type.ArrayType) VarcharType(com.facebook.presto.common.type.VarcharType) MapType(com.facebook.presto.common.type.MapType) ArrayType(com.facebook.presto.common.type.ArrayType) CharType(com.facebook.presto.common.type.CharType) Type(com.facebook.presto.common.type.Type) RowType(com.facebook.presto.common.type.RowType) Entry(java.util.Map.Entry) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) CharType(com.facebook.presto.common.type.CharType) TimeZoneKey(com.facebook.presto.common.type.TimeZoneKey) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) BlockBuilder(com.facebook.presto.common.block.BlockBuilder)

Example 32 with VarcharType

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

the class DruidPushdownUtils method getLiteralAsString.

// Copied from com.facebook.presto.sql.planner.LiteralInterpreter.evaluate
public static String getLiteralAsString(ConnectorSession session, ConstantExpression node) {
    Type type = node.getType();
    if (node.getValue() == null) {
        throw new PrestoException(DRUID_PUSHDOWN_UNSUPPORTED_EXPRESSION, "Null constant expression: " + node + " with value of type: " + type);
    }
    if (type instanceof BooleanType) {
        return String.valueOf(((Boolean) node.getValue()).booleanValue());
    }
    if (type instanceof BigintType || type instanceof TinyintType || type instanceof SmallintType || type instanceof IntegerType) {
        Number number = (Number) node.getValue();
        return format("%d", number.longValue());
    }
    if (type instanceof DoubleType) {
        return node.getValue().toString();
    }
    if (type instanceof RealType) {
        Long number = (Long) node.getValue();
        return format("%f", 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).toString();
        }
        checkState(node.getValue() instanceof Slice);
        Slice value = (Slice) node.getValue();
        return decodeDecimal(decodeUnscaledValue(value), decimalType).toString();
    }
    if (type instanceof VarcharType || type instanceof CharType) {
        return "'" + ((Slice) node.getValue()).toStringUtf8() + "'";
    }
    if (type instanceof TimestampType) {
        return getTimestampLiteralAsString(session, (long) node.getValue());
    }
    if (type instanceof TimestampWithTimeZoneType) {
        return getTimestampLiteralAsString(session, new SqlTimestampWithTimeZone((long) node.getValue()).getMillisUtc());
    }
    throw new PrestoException(DRUID_PUSHDOWN_UNSUPPORTED_EXPRESSION, "Cannot handle the constant expression: " + node + " with value of type: " + type);
}
Also used : VarcharType(com.facebook.presto.common.type.VarcharType) TinyintType(com.facebook.presto.common.type.TinyintType) BooleanType(com.facebook.presto.common.type.BooleanType) PrestoException(com.facebook.presto.spi.PrestoException) RealType(com.facebook.presto.common.type.RealType) BigintType(com.facebook.presto.common.type.BigintType) IntegerType(com.facebook.presto.common.type.IntegerType) DecimalType(com.facebook.presto.common.type.DecimalType) BooleanType(com.facebook.presto.common.type.BooleanType) IntegerType(com.facebook.presto.common.type.IntegerType) CharType(com.facebook.presto.common.type.CharType) Type(com.facebook.presto.common.type.Type) TinyintType(com.facebook.presto.common.type.TinyintType) BigintType(com.facebook.presto.common.type.BigintType) VarcharType(com.facebook.presto.common.type.VarcharType) RealType(com.facebook.presto.common.type.RealType) TimestampWithTimeZoneType(com.facebook.presto.common.type.TimestampWithTimeZoneType) SmallintType(com.facebook.presto.common.type.SmallintType) DoubleType(com.facebook.presto.common.type.DoubleType) TimestampType(com.facebook.presto.common.type.TimestampType) SqlTimestampWithTimeZone(com.facebook.presto.common.type.SqlTimestampWithTimeZone) DoubleType(com.facebook.presto.common.type.DoubleType) Slice(io.airlift.slice.Slice) TimestampWithTimeZoneType(com.facebook.presto.common.type.TimestampWithTimeZoneType) DecimalType(com.facebook.presto.common.type.DecimalType) TimestampType(com.facebook.presto.common.type.TimestampType) SmallintType(com.facebook.presto.common.type.SmallintType) CharType(com.facebook.presto.common.type.CharType)

Example 33 with VarcharType

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

the class HivePartitionManager method createPartitionPredicates.

private Map<Column, Domain> createPartitionPredicates(SemiTransactionalHiveMetastore metastore, ConnectorSession session, TupleDomain<ColumnHandle> effectivePredicateColumnHandles, List<HiveColumnHandle> partitionColumns, boolean assumeCanonicalPartitionKeys) {
    Optional<Map<ColumnHandle, Domain>> domains = effectivePredicateColumnHandles.getDomains();
    if (domains.isPresent()) {
        Map<ColumnHandle, Domain> columnHandleDomainMap = domains.get();
        ImmutableMap.Builder<Column, Domain> partitionPredicateBuilder = ImmutableMap.builder();
        MetastoreContext metastoreContext = new MetastoreContext(session.getIdentity(), session.getQueryId(), session.getClientInfo(), session.getSource(), getMetastoreHeaders(session), isUserDefinedTypeEncodingEnabled(session), metastore.getColumnConverterProvider());
        for (HiveColumnHandle partitionColumn : partitionColumns) {
            Column key = new Column(partitionColumn.getName(), partitionColumn.getHiveType(), partitionColumn.getComment(), metastoreContext.getColumnConverter().getTypeMetadata(partitionColumn.getHiveType(), partitionColumn.getTypeSignature()));
            if (columnHandleDomainMap.containsKey(partitionColumn)) {
                if (assumeCanonicalPartitionKeys) {
                    partitionPredicateBuilder.put(key, columnHandleDomainMap.get(partitionColumn));
                } else {
                    Type type = typeManager.getType(partitionColumn.getTypeSignature());
                    if (type instanceof VarcharType || type instanceof CharType) {
                        partitionPredicateBuilder.put(key, columnHandleDomainMap.get(partitionColumn));
                    } else {
                        Domain allDomain = Domain.all(typeManager.getType(partitionColumn.getTypeSignature()));
                        partitionPredicateBuilder.put(key, allDomain);
                    }
                }
            } else {
                Domain allDomain = Domain.all(typeManager.getType(partitionColumn.getTypeSignature()));
                partitionPredicateBuilder.put(key, allDomain);
            }
        }
        return partitionPredicateBuilder.build();
    } else {
        return ImmutableMap.of();
    }
}
Also used : ColumnHandle(com.facebook.presto.spi.ColumnHandle) VarcharType(com.facebook.presto.common.type.VarcharType) MetastoreContext(com.facebook.presto.hive.metastore.MetastoreContext) ImmutableMap(com.google.common.collect.ImmutableMap) VarcharType(com.facebook.presto.common.type.VarcharType) CharType(com.facebook.presto.common.type.CharType) Type(com.facebook.presto.common.type.Type) Column(com.facebook.presto.hive.metastore.Column) CharType(com.facebook.presto.common.type.CharType) Domain(com.facebook.presto.common.predicate.Domain) TupleDomain(com.facebook.presto.common.predicate.TupleDomain) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 34 with VarcharType

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

the class OrcTester method writeValue.

private static void writeValue(Type type, BlockBuilder blockBuilder, Object value) {
    if (value == null) {
        blockBuilder.appendNull();
    } else {
        if (BOOLEAN.equals(type)) {
            type.writeBoolean(blockBuilder, (Boolean) value);
        } else if (TINYINT.equals(type) || SMALLINT.equals(type) || INTEGER.equals(type) || BIGINT.equals(type)) {
            type.writeLong(blockBuilder, ((Number) value).longValue());
        } else if (Decimals.isShortDecimal(type)) {
            type.writeLong(blockBuilder, ((SqlDecimal) value).toBigDecimal().unscaledValue().longValue());
        } else if (Decimals.isLongDecimal(type)) {
            type.writeSlice(blockBuilder, Decimals.encodeUnscaledValue(((SqlDecimal) value).toBigDecimal().unscaledValue()));
        } else if (DOUBLE.equals(type)) {
            type.writeDouble(blockBuilder, ((Number) value).doubleValue());
        } else if (REAL.equals(type)) {
            float floatValue = ((Number) value).floatValue();
            type.writeLong(blockBuilder, Float.floatToIntBits(floatValue));
        } else if (type instanceof VarcharType) {
            Slice slice = truncateToLength(utf8Slice((String) value), type);
            type.writeSlice(blockBuilder, slice);
        } else if (type instanceof CharType) {
            Slice slice = truncateToLengthAndTrimSpaces(utf8Slice((String) value), type);
            type.writeSlice(blockBuilder, slice);
        } else if (VARBINARY.equals(type)) {
            type.writeSlice(blockBuilder, Slices.wrappedBuffer(((SqlVarbinary) value).getBytes()));
        } else if (DATE.equals(type)) {
            long days = ((SqlDate) value).getDays();
            type.writeLong(blockBuilder, days);
        } else if (TIMESTAMP.equals(type)) {
            long millis = ((SqlTimestamp) value).getMillisUtc();
            type.writeLong(blockBuilder, millis);
        } else {
            String baseType = type.getTypeSignature().getBase();
            if (StandardTypes.ARRAY.equals(baseType)) {
                List<?> array = (List<?>) value;
                Type elementType = type.getTypeParameters().get(0);
                BlockBuilder arrayBlockBuilder = blockBuilder.beginBlockEntry();
                for (Object elementValue : array) {
                    writeValue(elementType, arrayBlockBuilder, elementValue);
                }
                blockBuilder.closeEntry();
            } else if (StandardTypes.MAP.equals(baseType)) {
                Map<?, ?> map = (Map<?, ?>) value;
                Type keyType = type.getTypeParameters().get(0);
                Type valueType = type.getTypeParameters().get(1);
                BlockBuilder mapBlockBuilder = blockBuilder.beginBlockEntry();
                for (Entry<?, ?> entry : map.entrySet()) {
                    writeValue(keyType, mapBlockBuilder, entry.getKey());
                    writeValue(valueType, mapBlockBuilder, entry.getValue());
                }
                blockBuilder.closeEntry();
            } else if (StandardTypes.ROW.equals(baseType)) {
                List<?> array = (List<?>) value;
                List<Type> fieldTypes = type.getTypeParameters();
                BlockBuilder rowBlockBuilder = blockBuilder.beginBlockEntry();
                for (int fieldId = 0; fieldId < fieldTypes.size(); fieldId++) {
                    Type fieldType = fieldTypes.get(fieldId);
                    writeValue(fieldType, rowBlockBuilder, array.get(fieldId));
                }
                blockBuilder.closeEntry();
            } else {
                throw new IllegalArgumentException("Unsupported type " + type);
            }
        }
    }
}
Also used : VarcharType(com.facebook.presto.common.type.VarcharType) SqlVarbinary(com.facebook.presto.common.type.SqlVarbinary) SqlDecimal(com.facebook.presto.common.type.SqlDecimal) SqlTimestamp(com.facebook.presto.common.type.SqlTimestamp) DecimalType(com.facebook.presto.common.type.DecimalType) ArrayType(com.facebook.presto.common.type.ArrayType) CharType(com.facebook.presto.common.type.CharType) RowType(com.facebook.presto.common.type.RowType) VarcharType(com.facebook.presto.common.type.VarcharType) VarbinaryType(com.facebook.presto.common.type.VarbinaryType) MapType(com.facebook.presto.common.type.MapType) Type(com.facebook.presto.common.type.Type) Entry(java.util.Map.Entry) Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) Slice(io.airlift.slice.Slice) Arrays.asList(java.util.Arrays.asList) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Collectors.toList(java.util.stream.Collectors.toList) OrcLazyObject(com.facebook.hive.orc.lazy.OrcLazyObject) CharType(com.facebook.presto.common.type.CharType) Map(java.util.Map) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) BlockBuilder(com.facebook.presto.common.block.BlockBuilder)

Example 35 with VarcharType

use of com.facebook.presto.common.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.common.type.VarcharType) DecimalType(com.facebook.presto.common.type.DecimalType) Chars.isCharType(com.facebook.presto.common.type.Chars.isCharType) CharType(com.facebook.presto.common.type.CharType)

Aggregations

VarcharType (com.facebook.presto.common.type.VarcharType)48 DecimalType (com.facebook.presto.common.type.DecimalType)30 Type (com.facebook.presto.common.type.Type)26 CharType (com.facebook.presto.common.type.CharType)23 PrestoException (com.facebook.presto.spi.PrestoException)16 Slice (io.airlift.slice.Slice)16 ArrayType (com.facebook.presto.common.type.ArrayType)14 RowType (com.facebook.presto.common.type.RowType)13 MapType (com.facebook.presto.common.type.MapType)12 ArrayList (java.util.ArrayList)12 ImmutableList (com.google.common.collect.ImmutableList)11 List (java.util.List)11 IntegerType (com.facebook.presto.common.type.IntegerType)10 TimestampType (com.facebook.presto.common.type.TimestampType)10 VarbinaryType (com.facebook.presto.common.type.VarbinaryType)10 BigintType (com.facebook.presto.common.type.BigintType)9 BooleanType (com.facebook.presto.common.type.BooleanType)9 DoubleType (com.facebook.presto.common.type.DoubleType)9 RealType (com.facebook.presto.common.type.RealType)9 SmallintType (com.facebook.presto.common.type.SmallintType)9