Search in sources :

Example 6 with CharType

use of io.trino.spi.type.CharType in project trino by trinodb.

the class HiveWriteUtils method getField.

public static Object getField(DateTimeZone localZone, Type type, Block block, int position) {
    if (block.isNull(position)) {
        return null;
    }
    if (BOOLEAN.equals(type)) {
        return type.getBoolean(block, position);
    }
    if (BIGINT.equals(type)) {
        return type.getLong(block, position);
    }
    if (INTEGER.equals(type)) {
        return toIntExact(type.getLong(block, position));
    }
    if (SMALLINT.equals(type)) {
        return Shorts.checkedCast(type.getLong(block, position));
    }
    if (TINYINT.equals(type)) {
        return SignedBytes.checkedCast(type.getLong(block, position));
    }
    if (REAL.equals(type)) {
        return intBitsToFloat((int) type.getLong(block, position));
    }
    if (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(padSpaces(type.getSlice(block, position), charType).toStringUtf8());
    }
    if (VARBINARY.equals(type)) {
        return type.getSlice(block, position).getBytes();
    }
    if (DATE.equals(type)) {
        return Date.ofEpochDay(toIntExact(type.getLong(block, position)));
    }
    if (type instanceof TimestampType) {
        return getHiveTimestamp(localZone, (TimestampType) type, block, position);
    }
    if (type instanceof DecimalType) {
        DecimalType decimalType = (DecimalType) type;
        return getHiveDecimal(decimalType, block, position);
    }
    if (type instanceof ArrayType) {
        Type elementType = ((ArrayType) type).getElementType();
        Block arrayBlock = block.getObject(position, Block.class);
        List<Object> list = new ArrayList<>(arrayBlock.getPositionCount());
        for (int i = 0; i < arrayBlock.getPositionCount(); i++) {
            list.add(getField(localZone, elementType, arrayBlock, i));
        }
        return unmodifiableList(list);
    }
    if (type instanceof MapType) {
        Type keyType = ((MapType) type).getKeyType();
        Type valueType = ((MapType) type).getValueType();
        Block mapBlock = block.getObject(position, Block.class);
        Map<Object, Object> map = new HashMap<>();
        for (int i = 0; i < mapBlock.getPositionCount(); i += 2) {
            map.put(getField(localZone, keyType, mapBlock, i), getField(localZone, valueType, mapBlock, i + 1));
        }
        return unmodifiableMap(map);
    }
    if (type instanceof RowType) {
        List<Type> fieldTypes = type.getTypeParameters();
        Block rowBlock = block.getObject(position, Block.class);
        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++) {
            row.add(getField(localZone, fieldTypes.get(i), rowBlock, i));
        }
        return unmodifiableList(row);
    }
    throw new TrinoException(NOT_SUPPORTED, "unsupported type: " + type);
}
Also used : VarcharType(io.trino.spi.type.VarcharType) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) HiveUtil.isRowType(io.trino.plugin.hive.util.HiveUtil.isRowType) RowType(io.trino.spi.type.RowType) Text(org.apache.hadoop.io.Text) HiveUtil.isMapType(io.trino.plugin.hive.util.HiveUtil.isMapType) MapType(io.trino.spi.type.MapType) ArrayType(io.trino.spi.type.ArrayType) HiveUtil.isArrayType(io.trino.plugin.hive.util.HiveUtil.isArrayType) HiveUtil.isMapType(io.trino.plugin.hive.util.HiveUtil.isMapType) HiveUtil.isRowType(io.trino.plugin.hive.util.HiveUtil.isRowType) TimestampType(io.trino.spi.type.TimestampType) HiveType(io.trino.plugin.hive.HiveType) MapType(io.trino.spi.type.MapType) CharType(io.trino.spi.type.CharType) RowType(io.trino.spi.type.RowType) ArrayType(io.trino.spi.type.ArrayType) DecimalType(io.trino.spi.type.DecimalType) Type(io.trino.spi.type.Type) VarcharType(io.trino.spi.type.VarcharType) HiveUtil.isArrayType(io.trino.plugin.hive.util.HiveUtil.isArrayType) TimestampType(io.trino.spi.type.TimestampType) DecimalType(io.trino.spi.type.DecimalType) Block(io.trino.spi.block.Block) TrinoException(io.trino.spi.TrinoException) CharType(io.trino.spi.type.CharType)

Example 7 with CharType

use of io.trino.spi.type.CharType in project trino by trinodb.

the class SerDeUtils method serializePrimitive.

private static void serializePrimitive(Type type, BlockBuilder builder, Object object, PrimitiveObjectInspector inspector) {
    requireNonNull(builder, "builder is null");
    if (object == null) {
        builder.appendNull();
        return;
    }
    switch(inspector.getPrimitiveCategory()) {
        case BOOLEAN:
            type.writeBoolean(builder, ((BooleanObjectInspector) inspector).get(object));
            return;
        case BYTE:
            type.writeLong(builder, ((ByteObjectInspector) inspector).get(object));
            return;
        case SHORT:
            type.writeLong(builder, ((ShortObjectInspector) inspector).get(object));
            return;
        case INT:
            type.writeLong(builder, ((IntObjectInspector) inspector).get(object));
            return;
        case LONG:
            type.writeLong(builder, ((LongObjectInspector) inspector).get(object));
            return;
        case FLOAT:
            type.writeLong(builder, floatToRawIntBits(((FloatObjectInspector) inspector).get(object)));
            return;
        case DOUBLE:
            type.writeDouble(builder, ((DoubleObjectInspector) inspector).get(object));
            return;
        case STRING:
            type.writeSlice(builder, Slices.utf8Slice(((StringObjectInspector) inspector).getPrimitiveJavaObject(object)));
            return;
        case VARCHAR:
            type.writeSlice(builder, Slices.utf8Slice(((HiveVarcharObjectInspector) inspector).getPrimitiveJavaObject(object).getValue()));
            return;
        case CHAR:
            HiveChar hiveChar = ((HiveCharObjectInspector) inspector).getPrimitiveJavaObject(object);
            type.writeSlice(builder, truncateToLengthAndTrimSpaces(Slices.utf8Slice(hiveChar.getValue()), ((CharType) type).getLength()));
            return;
        case DATE:
            type.writeLong(builder, formatDateAsLong(object, (DateObjectInspector) inspector));
            return;
        case TIMESTAMP:
            TimestampType timestampType = (TimestampType) type;
            DecodedTimestamp timestamp = formatTimestamp(timestampType, object, (TimestampObjectInspector) inspector);
            createTimestampEncoder(timestampType, DateTimeZone.UTC).write(timestamp, builder);
            return;
        case BINARY:
            type.writeSlice(builder, Slices.wrappedBuffer(((BinaryObjectInspector) inspector).getPrimitiveJavaObject(object)));
            return;
        case DECIMAL:
            DecimalType decimalType = (DecimalType) type;
            HiveDecimalWritable hiveDecimal = ((HiveDecimalObjectInspector) inspector).getPrimitiveWritableObject(object);
            if (decimalType.isShort()) {
                type.writeLong(builder, DecimalUtils.getShortDecimalValue(hiveDecimal, decimalType.getScale()));
            } else {
                type.writeObject(builder, DecimalUtils.getLongDecimalValue(hiveDecimal, decimalType.getScale()));
            }
            return;
        case VOID:
        case TIMESTAMPLOCALTZ:
        case INTERVAL_YEAR_MONTH:
        case INTERVAL_DAY_TIME:
        case UNKNOWN:
    }
    throw new RuntimeException("Unknown primitive type: " + inspector.getPrimitiveCategory());
}
Also used : DateObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.DateObjectInspector) HiveDecimalWritable(org.apache.hadoop.hive.serde2.io.HiveDecimalWritable) HiveChar(org.apache.hadoop.hive.common.type.HiveChar) BinaryObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.BinaryObjectInspector) StringObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.StringObjectInspector) FloatObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.FloatObjectInspector) DecodedTimestamp(io.trino.plugin.base.type.DecodedTimestamp) TimestampType(io.trino.spi.type.TimestampType) DecimalType(io.trino.spi.type.DecimalType) HiveDecimalObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.HiveDecimalObjectInspector) CharType(io.trino.spi.type.CharType) HiveCharObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.HiveCharObjectInspector)

Example 8 with CharType

use of io.trino.spi.type.CharType in project trino by trinodb.

the class TestCharType method testGetObjectValue.

@Test
public void testGetObjectValue() {
    CharType charType = createCharType(3);
    for (int codePoint : ImmutableList.of(0, 1, 10, 17, (int) ' ', 127, 1011, 11_000, 65_891, MIN_SUPPLEMENTARY_CODE_POINT, MAX_CODE_POINT)) {
        BlockBuilder blockBuilder = charType.createBlockBuilder(null, 1);
        Slice slice = (codePoint != ' ') ? codePointToUtf8(codePoint) : EMPTY_SLICE;
        blockBuilder.writeBytes(slice, 0, slice.length());
        blockBuilder.closeEntry();
        Block block = blockBuilder.build();
        int codePointLengthInUtf16 = isSupplementaryCodePoint(codePoint) ? 2 : 1;
        String objectValue = (String) charType.getObjectValue(SESSION, block, 0);
        assertNotNull(objectValue);
        assertEquals(objectValue.codePointAt(0), codePoint, "first code point");
        assertEquals(objectValue.length(), codePointLengthInUtf16 + 2, "size");
        for (int i = codePointLengthInUtf16; i < objectValue.length(); i++) {
            assertEquals(objectValue.codePointAt(i), ' ');
        }
    }
}
Also used : Slice(io.airlift.slice.Slice) Block(io.trino.spi.block.Block) CharType.createCharType(io.trino.spi.type.CharType.createCharType) CharType(io.trino.spi.type.CharType) Character.isSupplementaryCodePoint(java.lang.Character.isSupplementaryCodePoint) BlockBuilder(io.trino.spi.block.BlockBuilder) Test(org.testng.annotations.Test)

Example 9 with CharType

use of io.trino.spi.type.CharType in project trino by trinodb.

the class DruidJdbcClient method legacyToWriteMapping.

private WriteMapping legacyToWriteMapping(Type type) {
    // This method is copied from deprecated BaseJdbcClient.legacyToWriteMapping()
    if (type == BOOLEAN) {
        return WriteMapping.booleanMapping("boolean", booleanWriteFunction());
    }
    if (type == TINYINT) {
        return WriteMapping.longMapping("tinyint", tinyintWriteFunction());
    }
    if (type == SMALLINT) {
        return WriteMapping.longMapping("smallint", smallintWriteFunction());
    }
    if (type == INTEGER) {
        return WriteMapping.longMapping("integer", integerWriteFunction());
    }
    if (type == BIGINT) {
        return WriteMapping.longMapping("bigint", bigintWriteFunction());
    }
    if (type == REAL) {
        return WriteMapping.longMapping("real", realWriteFunction());
    }
    if (type == DOUBLE) {
        return WriteMapping.doubleMapping("double precision", doubleWriteFunction());
    }
    if (type instanceof DecimalType) {
        DecimalType decimalType = (DecimalType) type;
        String dataType = format("decimal(%s, %s)", decimalType.getPrecision(), decimalType.getScale());
        if (decimalType.isShort()) {
            return WriteMapping.longMapping(dataType, shortDecimalWriteFunction(decimalType));
        }
        return WriteMapping.objectMapping(dataType, longDecimalWriteFunction(decimalType));
    }
    if (type instanceof CharType) {
        return WriteMapping.sliceMapping("char(" + ((CharType) type).getLength() + ")", charWriteFunction());
    }
    if (type instanceof VarcharType) {
        VarcharType varcharType = (VarcharType) type;
        String dataType;
        if (varcharType.isUnbounded()) {
            dataType = "varchar";
        } else {
            dataType = "varchar(" + varcharType.getBoundedLength() + ")";
        }
        return WriteMapping.sliceMapping(dataType, varcharWriteFunction());
    }
    if (type == VARBINARY) {
        return WriteMapping.sliceMapping("varbinary", varbinaryWriteFunction());
    }
    if (type == DATE) {
        return WriteMapping.longMapping("date", dateWriteFunctionUsingSqlDate());
    }
    throw new TrinoException(NOT_SUPPORTED, "Unsupported column type: " + type.getDisplayName());
}
Also used : VarcharType.createUnboundedVarcharType(io.trino.spi.type.VarcharType.createUnboundedVarcharType) VarcharType(io.trino.spi.type.VarcharType) DecimalType(io.trino.spi.type.DecimalType) DecimalType.createDecimalType(io.trino.spi.type.DecimalType.createDecimalType) TrinoException(io.trino.spi.TrinoException) CharType(io.trino.spi.type.CharType)

Example 10 with CharType

use of io.trino.spi.type.CharType in project trino by trinodb.

the class FormatFunction method valueConverter.

private static BiFunction<ConnectorSession, Block, Object> valueConverter(FunctionDependencies functionDependencies, Type type, int position) {
    if (type.equals(UNKNOWN)) {
        return (session, block) -> null;
    }
    if (type.equals(BOOLEAN)) {
        return (session, block) -> type.getBoolean(block, position);
    }
    if (type.equals(TINYINT) || type.equals(SMALLINT) || type.equals(INTEGER) || type.equals(BIGINT)) {
        return (session, block) -> type.getLong(block, position);
    }
    if (type.equals(REAL)) {
        return (session, block) -> intBitsToFloat(toIntExact(type.getLong(block, position)));
    }
    if (type.equals(DOUBLE)) {
        return (session, block) -> type.getDouble(block, position);
    }
    if (type.equals(DATE)) {
        return (session, block) -> LocalDate.ofEpochDay(type.getLong(block, position));
    }
    if (type instanceof TimestampWithTimeZoneType) {
        return (session, block) -> toZonedDateTime(((TimestampWithTimeZoneType) type), block, position);
    }
    if (type instanceof TimestampType) {
        return (session, block) -> toLocalDateTime(((TimestampType) type), block, position);
    }
    if (type instanceof TimeType) {
        return (session, block) -> toLocalTime(type.getLong(block, position));
    }
    // TODO: support TIME WITH TIME ZONE by https://github.com/trinodb/trino/issues/191 + mapping to java.time.OffsetTime
    if (type.equals(JSON)) {
        MethodHandle handle = functionDependencies.getFunctionInvoker(QualifiedName.of("json_format"), ImmutableList.of(JSON), simpleConvention(FAIL_ON_NULL, NEVER_NULL)).getMethodHandle();
        return (session, block) -> convertToString(handle, type.getSlice(block, position));
    }
    if (isShortDecimal(type)) {
        int scale = ((DecimalType) type).getScale();
        return (session, block) -> BigDecimal.valueOf(type.getLong(block, position), scale);
    }
    if (isLongDecimal(type)) {
        int scale = ((DecimalType) type).getScale();
        return (session, block) -> new BigDecimal(((Int128) type.getObject(block, position)).toBigInteger(), scale);
    }
    if (type instanceof VarcharType) {
        return (session, block) -> type.getSlice(block, position).toStringUtf8();
    }
    if (type instanceof CharType) {
        CharType charType = (CharType) type;
        return (session, block) -> padSpaces(type.getSlice(block, position), charType).toStringUtf8();
    }
    BiFunction<ConnectorSession, Block, Object> function;
    if (type.getJavaType() == long.class) {
        function = (session, block) -> type.getLong(block, position);
    } else if (type.getJavaType() == double.class) {
        function = (session, block) -> type.getDouble(block, position);
    } else if (type.getJavaType() == boolean.class) {
        function = (session, block) -> type.getBoolean(block, position);
    } else if (type.getJavaType() == Slice.class) {
        function = (session, block) -> type.getSlice(block, position);
    } else {
        function = (session, block) -> type.getObject(block, position);
    }
    MethodHandle handle = functionDependencies.getCastInvoker(type, VARCHAR, simpleConvention(FAIL_ON_NULL, NEVER_NULL)).getMethodHandle();
    return (session, block) -> convertToString(handle, function.apply(session, block));
}
Also used : FunctionDependencies(io.trino.metadata.FunctionDependencies) SCALAR(io.trino.metadata.FunctionKind.SCALAR) FunctionDependencyDeclarationBuilder(io.trino.metadata.FunctionDependencyDeclaration.FunctionDependencyDeclarationBuilder) FAIL_ON_NULL(io.trino.spi.function.InvocationConvention.InvocationReturnConvention.FAIL_ON_NULL) BiFunction(java.util.function.BiFunction) FunctionNullability(io.trino.metadata.FunctionNullability) UNKNOWN(io.trino.type.UnknownType.UNKNOWN) InvocationConvention.simpleConvention(io.trino.spi.function.InvocationConvention.simpleConvention) Timestamps.roundDiv(io.trino.spi.type.Timestamps.roundDiv) BigDecimal(java.math.BigDecimal) TimestampWithTimeZoneType(io.trino.spi.type.TimestampWithTimeZoneType) Block(io.trino.spi.block.Block) LocalTime(java.time.LocalTime) IllegalFormatException(java.util.IllegalFormatException) Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) INTEGER(io.trino.spi.type.IntegerType.INTEGER) FunctionMetadata(io.trino.metadata.FunctionMetadata) SMALLINT(io.trino.spi.type.SmallintType.SMALLINT) TypeSignature(io.trino.spi.type.TypeSignature) DateTimes.toLocalDateTime(io.trino.type.DateTimes.toLocalDateTime) FunctionDependencyDeclaration(io.trino.metadata.FunctionDependencyDeclaration) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) PICOSECONDS_PER_NANOSECOND(io.trino.type.DateTimes.PICOSECONDS_PER_NANOSECOND) TrinoException(io.trino.spi.TrinoException) String.format(java.lang.String.format) List(java.util.List) BIGINT(io.trino.spi.type.BigintType.BIGINT) INVALID_FUNCTION_ARGUMENT(io.trino.spi.StandardErrorCode.INVALID_FUNCTION_ARGUMENT) LocalDate(java.time.LocalDate) DecimalType(io.trino.spi.type.DecimalType) NEVER_NULL(io.trino.spi.function.InvocationConvention.InvocationArgumentConvention.NEVER_NULL) DATE(io.trino.spi.type.DateType.DATE) REAL(io.trino.spi.type.RealType.REAL) MethodHandle(java.lang.invoke.MethodHandle) Slice(io.airlift.slice.Slice) TimeType(io.trino.spi.type.TimeType) Decimals.isLongDecimal(io.trino.spi.type.Decimals.isLongDecimal) Type(io.trino.spi.type.Type) BOOLEAN(io.trino.spi.type.BooleanType.BOOLEAN) Float.intBitsToFloat(java.lang.Float.intBitsToFloat) DateTimes.toZonedDateTime(io.trino.type.DateTimes.toZonedDateTime) TimestampType(io.trino.spi.type.TimestampType) VarcharType(io.trino.spi.type.VarcharType) VARCHAR(io.trino.spi.type.VarcharType.VARCHAR) ImmutableList(com.google.common.collect.ImmutableList) Chars.padSpaces(io.trino.spi.type.Chars.padSpaces) Math.toIntExact(java.lang.Math.toIntExact) Signature(io.trino.metadata.Signature) Decimals.isShortDecimal(io.trino.spi.type.Decimals.isShortDecimal) Int128(io.trino.spi.type.Int128) Streams.mapWithIndex(com.google.common.collect.Streams.mapWithIndex) ConnectorSession(io.trino.spi.connector.ConnectorSession) Signature.withVariadicBound(io.trino.metadata.Signature.withVariadicBound) UsedByGeneratedCode(io.trino.annotation.UsedByGeneratedCode) SqlScalarFunction(io.trino.metadata.SqlScalarFunction) Failures.internalError(io.trino.util.Failures.internalError) QualifiedName(io.trino.sql.tree.QualifiedName) DOUBLE(io.trino.spi.type.DoubleType.DOUBLE) CharType(io.trino.spi.type.CharType) BoundSignature(io.trino.metadata.BoundSignature) TINYINT(io.trino.spi.type.TinyintType.TINYINT) JSON(io.trino.type.JsonType.JSON) Reflection.methodHandle(io.trino.util.Reflection.methodHandle) VarcharType(io.trino.spi.type.VarcharType) BigDecimal(java.math.BigDecimal) TimeType(io.trino.spi.type.TimeType) Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) Slice(io.airlift.slice.Slice) TimestampWithTimeZoneType(io.trino.spi.type.TimestampWithTimeZoneType) TimestampType(io.trino.spi.type.TimestampType) DecimalType(io.trino.spi.type.DecimalType) Block(io.trino.spi.block.Block) ConnectorSession(io.trino.spi.connector.ConnectorSession) CharType(io.trino.spi.type.CharType) MethodHandle(java.lang.invoke.MethodHandle)

Aggregations

CharType (io.trino.spi.type.CharType)50 VarcharType (io.trino.spi.type.VarcharType)45 DecimalType (io.trino.spi.type.DecimalType)39 TrinoException (io.trino.spi.TrinoException)28 ArrayType (io.trino.spi.type.ArrayType)20 TimestampType (io.trino.spi.type.TimestampType)20 Type (io.trino.spi.type.Type)19 DecimalType.createDecimalType (io.trino.spi.type.DecimalType.createDecimalType)16 Slice (io.airlift.slice.Slice)13 TimeType (io.trino.spi.type.TimeType)12 VarbinaryType (io.trino.spi.type.VarbinaryType)12 VarcharType.createUnboundedVarcharType (io.trino.spi.type.VarcharType.createUnboundedVarcharType)12 MapType (io.trino.spi.type.MapType)11 RowType (io.trino.spi.type.RowType)11 ImmutableList (com.google.common.collect.ImmutableList)10 TimestampWithTimeZoneType (io.trino.spi.type.TimestampWithTimeZoneType)10 Slices.utf8Slice (io.airlift.slice.Slices.utf8Slice)9 ArrayList (java.util.ArrayList)9 List (java.util.List)9 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)8