Search in sources :

Example 1 with VarbinaryType

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

the class HiveUtil method parsePartitionValue.

public static NullableValue parsePartitionValue(String partitionName, String value, Type type) {
    verifyPartitionTypeSupported(partitionName, type);
    boolean isNull = HIVE_DEFAULT_DYNAMIC_PARTITION.equals(value);
    if (type instanceof DecimalType) {
        DecimalType decimalType = (DecimalType) type;
        if (isNull) {
            return NullableValue.asNull(decimalType);
        }
        if (decimalType.isShort()) {
            if (value.isEmpty()) {
                return NullableValue.of(decimalType, 0L);
            }
            return NullableValue.of(decimalType, shortDecimalPartitionKey(value, decimalType, partitionName));
        } else {
            if (value.isEmpty()) {
                return NullableValue.of(decimalType, Int128.ZERO);
            }
            return NullableValue.of(decimalType, longDecimalPartitionKey(value, decimalType, partitionName));
        }
    }
    if (BOOLEAN.equals(type)) {
        if (isNull) {
            return NullableValue.asNull(BOOLEAN);
        }
        if (value.isEmpty()) {
            return NullableValue.of(BOOLEAN, false);
        }
        return NullableValue.of(BOOLEAN, booleanPartitionKey(value, partitionName));
    }
    if (TINYINT.equals(type)) {
        if (isNull) {
            return NullableValue.asNull(TINYINT);
        }
        if (value.isEmpty()) {
            return NullableValue.of(TINYINT, 0L);
        }
        return NullableValue.of(TINYINT, tinyintPartitionKey(value, partitionName));
    }
    if (SMALLINT.equals(type)) {
        if (isNull) {
            return NullableValue.asNull(SMALLINT);
        }
        if (value.isEmpty()) {
            return NullableValue.of(SMALLINT, 0L);
        }
        return NullableValue.of(SMALLINT, smallintPartitionKey(value, partitionName));
    }
    if (INTEGER.equals(type)) {
        if (isNull) {
            return NullableValue.asNull(INTEGER);
        }
        if (value.isEmpty()) {
            return NullableValue.of(INTEGER, 0L);
        }
        return NullableValue.of(INTEGER, integerPartitionKey(value, partitionName));
    }
    if (BIGINT.equals(type)) {
        if (isNull) {
            return NullableValue.asNull(BIGINT);
        }
        if (value.isEmpty()) {
            return NullableValue.of(BIGINT, 0L);
        }
        return NullableValue.of(BIGINT, bigintPartitionKey(value, partitionName));
    }
    if (DATE.equals(type)) {
        if (isNull) {
            return NullableValue.asNull(DATE);
        }
        return NullableValue.of(DATE, datePartitionKey(value, partitionName));
    }
    if (TIMESTAMP_MILLIS.equals(type)) {
        if (isNull) {
            return NullableValue.asNull(TIMESTAMP_MILLIS);
        }
        return NullableValue.of(TIMESTAMP_MILLIS, timestampPartitionKey(value, partitionName));
    }
    if (REAL.equals(type)) {
        if (isNull) {
            return NullableValue.asNull(REAL);
        }
        if (value.isEmpty()) {
            return NullableValue.of(REAL, (long) floatToRawIntBits(0.0f));
        }
        return NullableValue.of(REAL, floatPartitionKey(value, partitionName));
    }
    if (DOUBLE.equals(type)) {
        if (isNull) {
            return NullableValue.asNull(DOUBLE);
        }
        if (value.isEmpty()) {
            return NullableValue.of(DOUBLE, 0.0);
        }
        return NullableValue.of(DOUBLE, doublePartitionKey(value, partitionName));
    }
    if (type instanceof VarcharType) {
        if (isNull) {
            return NullableValue.asNull(type);
        }
        return NullableValue.of(type, varcharPartitionKey(value, partitionName, type));
    }
    if (type instanceof CharType) {
        if (isNull) {
            return NullableValue.asNull(type);
        }
        return NullableValue.of(type, charPartitionKey(value, partitionName, type));
    }
    if (type instanceof VarbinaryType) {
        if (isNull) {
            return NullableValue.asNull(type);
        }
        return NullableValue.of(type, Slices.utf8Slice(value));
    }
    throw new VerifyException(format("Unhandled type [%s] for partition: %s", type, partitionName));
}
Also used : VarbinaryType(io.trino.spi.type.VarbinaryType) VarcharType(io.trino.spi.type.VarcharType) VerifyException(com.google.common.base.VerifyException) DecimalType.createDecimalType(io.trino.spi.type.DecimalType.createDecimalType) DecimalType(io.trino.spi.type.DecimalType) CharType(io.trino.spi.type.CharType)

Example 2 with VarbinaryType

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

the class OrcTester method getJavaObjectInspector.

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_MILLIS) || type.equals(TIMESTAMP_MICROS) || type.equals(TIMESTAMP_NANOS)) {
        return javaTimestampObjectInspector;
    }
    if (type.equals(TIMESTAMP_TZ_MILLIS) || type.equals(TIMESTAMP_TZ_MICROS) || type.equals(TIMESTAMP_TZ_NANOS)) {
        return javaTimestampTZObjectInspector;
    }
    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);
}
Also used : JavaHiveCharObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.JavaHiveCharObjectInspector) PrimitiveObjectInspectorFactory.javaByteObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaByteObjectInspector) PrimitiveObjectInspectorFactory.javaLongObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaLongObjectInspector) PrimitiveObjectInspectorFactory.javaTimestampObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaTimestampObjectInspector) PrimitiveObjectInspectorFactory.javaDateObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaDateObjectInspector) ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) PrimitiveObjectInspectorFactory.javaByteArrayObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaByteArrayObjectInspector) PrimitiveObjectInspectorFactory.javaFloatObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaFloatObjectInspector) PrimitiveObjectInspectorFactory.javaDoubleObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaDoubleObjectInspector) PrimitiveObjectInspectorFactory.javaIntObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaIntObjectInspector) JavaHiveCharObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.JavaHiveCharObjectInspector) PrimitiveObjectInspectorFactory.javaTimestampTZObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaTimestampTZObjectInspector) PrimitiveObjectInspectorFactory.javaShortObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaShortObjectInspector) ObjectInspectorFactory.getStandardStructObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory.getStandardStructObjectInspector) SettableStructObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.SettableStructObjectInspector) StructObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector) PrimitiveObjectInspectorFactory.javaBooleanObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaBooleanObjectInspector) PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector) ObjectInspectorFactory.getStandardMapObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory.getStandardMapObjectInspector) ObjectInspectorFactory.getStandardListObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory.getStandardListObjectInspector) PrimitiveObjectInspectorFactory.javaStringObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaStringObjectInspector) VarcharType(io.trino.spi.type.VarcharType) RowType(io.trino.spi.type.RowType) MapType(io.trino.spi.type.MapType) DecimalTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo) ArrayType(io.trino.spi.type.ArrayType) VarbinaryType(io.trino.spi.type.VarbinaryType) DecimalType(io.trino.spi.type.DecimalType) CharType(io.trino.spi.type.CharType)

Example 3 with VarbinaryType

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

the class AbstractRowEncoder method appendColumnValue.

@Override
public void appendColumnValue(Block block, int position) {
    checkArgument(currentColumnIndex < columnHandles.size(), format("currentColumnIndex '%d' is greater than number of columns '%d'", currentColumnIndex, columnHandles.size()));
    Type type = columnHandles.get(currentColumnIndex).getType();
    if (block.isNull(position)) {
        appendNullValue();
    } else if (type == BOOLEAN) {
        appendBoolean(type.getBoolean(block, position));
    } else if (type == BIGINT) {
        appendLong(type.getLong(block, position));
    } else if (type == INTEGER) {
        appendInt(toIntExact(type.getLong(block, position)));
    } else if (type == SMALLINT) {
        appendShort(Shorts.checkedCast(type.getLong(block, position)));
    } else if (type == TINYINT) {
        appendByte(SignedBytes.checkedCast(type.getLong(block, position)));
    } else if (type == DOUBLE) {
        appendDouble(type.getDouble(block, position));
    } else if (type == REAL) {
        appendFloat(intBitsToFloat(toIntExact(type.getLong(block, position))));
    } else if (type instanceof VarcharType) {
        appendString(type.getSlice(block, position).toStringUtf8());
    } else if (type instanceof VarbinaryType) {
        appendByteBuffer(type.getSlice(block, position).toByteBuffer());
    } else if (type == DATE) {
        appendSqlDate((SqlDate) type.getObjectValue(session, block, position));
    } else if (type instanceof TimeType) {
        appendSqlTime((SqlTime) type.getObjectValue(session, block, position));
    } else if (type instanceof TimeWithTimeZoneType) {
        appendSqlTimeWithTimeZone((SqlTimeWithTimeZone) type.getObjectValue(session, block, position));
    } else if (type instanceof TimestampType) {
        appendSqlTimestamp((SqlTimestamp) type.getObjectValue(session, block, position));
    } else if (type instanceof TimestampWithTimeZoneType) {
        appendSqlTimestampWithTimeZone((SqlTimestampWithTimeZone) type.getObjectValue(session, block, position));
    } else if (type instanceof ArrayType) {
        appendArray((List<Object>) type.getObjectValue(session, block, position));
    } else if (type instanceof MapType) {
        appendMap((Map<Object, Object>) type.getObjectValue(session, block, position));
    } else if (type instanceof RowType) {
        appendRow((List<Object>) type.getObjectValue(session, block, position));
    } else {
        throw new UnsupportedOperationException(format("Unsupported type '%s' for column '%s'", type, columnHandles.get(currentColumnIndex).getName()));
    }
    currentColumnIndex++;
}
Also used : VarcharType(io.trino.spi.type.VarcharType) SqlTime(io.trino.spi.type.SqlTime) TimeWithTimeZoneType(io.trino.spi.type.TimeWithTimeZoneType) RowType(io.trino.spi.type.RowType) SqlTimestamp(io.trino.spi.type.SqlTimestamp) MapType(io.trino.spi.type.MapType) TimeType(io.trino.spi.type.TimeType) ArrayType(io.trino.spi.type.ArrayType) TimeType(io.trino.spi.type.TimeType) Type(io.trino.spi.type.Type) TimestampType(io.trino.spi.type.TimestampType) VarcharType(io.trino.spi.type.VarcharType) TimestampWithTimeZoneType(io.trino.spi.type.TimestampWithTimeZoneType) RowType(io.trino.spi.type.RowType) MapType(io.trino.spi.type.MapType) ArrayType(io.trino.spi.type.ArrayType) VarbinaryType(io.trino.spi.type.VarbinaryType) TimeWithTimeZoneType(io.trino.spi.type.TimeWithTimeZoneType) VarbinaryType(io.trino.spi.type.VarbinaryType) TimestampWithTimeZoneType(io.trino.spi.type.TimestampWithTimeZoneType) TimestampType(io.trino.spi.type.TimestampType) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List)

Example 4 with VarbinaryType

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

the class ClickHouseClient method toWriteMapping.

@Override
public WriteMapping toWriteMapping(ConnectorSession session, Type type) {
    if (type == BOOLEAN) {
        // ClickHouse is no separate type for boolean values. Use UInt8 type, restricted to the values 0 or 1.
        return WriteMapping.booleanMapping("UInt8", booleanWriteFunction());
    }
    if (type == TINYINT) {
        return WriteMapping.longMapping("Int8", tinyintWriteFunction());
    }
    if (type == SMALLINT) {
        return WriteMapping.longMapping("Int16", smallintWriteFunction());
    }
    if (type == INTEGER) {
        return WriteMapping.longMapping("Int32", integerWriteFunction());
    }
    if (type == BIGINT) {
        return WriteMapping.longMapping("Int64", bigintWriteFunction());
    }
    if (type == REAL) {
        return WriteMapping.longMapping("Float32", realWriteFunction());
    }
    if (type == DOUBLE) {
        return WriteMapping.doubleMapping("Float64", 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 || type instanceof VarcharType) {
        // The String type replaces the types VARCHAR, BLOB, CLOB, and others from other DBMSs.
        return WriteMapping.sliceMapping("String", varcharWriteFunction());
    }
    if (type instanceof VarbinaryType) {
        // Strings of an arbitrary length. The length is not limited
        return WriteMapping.sliceMapping("String", varbinaryWriteFunction());
    }
    if (type == DATE) {
        // TODO (https://github.com/trinodb/trino/issues/10055) Deny unsupported dates to prevent inserting wrong values. 2106-02-07 is max value in version 20.8
        return WriteMapping.longMapping("Date", dateWriteFunctionUsingLocalDate());
    }
    if (type == TIMESTAMP_SECONDS) {
        return WriteMapping.longMapping("DateTime", timestampSecondsWriteFunction());
    }
    if (type.equals(uuidType)) {
        return WriteMapping.sliceMapping("UUID", uuidWriteFunction());
    }
    throw new TrinoException(NOT_SUPPORTED, "Unsupported column type: " + type);
}
Also used : VarbinaryType(io.trino.spi.type.VarbinaryType) VarcharType.createUnboundedVarcharType(io.trino.spi.type.VarcharType.createUnboundedVarcharType) VarcharType(io.trino.spi.type.VarcharType) DecimalType.createDecimalType(io.trino.spi.type.DecimalType.createDecimalType) DecimalType(io.trino.spi.type.DecimalType) TrinoException(io.trino.spi.TrinoException) CharType(io.trino.spi.type.CharType)

Example 5 with VarbinaryType

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

the class ParquetSchemaConverter method getPrimitiveType.

private org.apache.parquet.schema.Type getPrimitiveType(Type type, String name, List<String> parent) {
    List<String> fullName = ImmutableList.<String>builder().addAll(parent).add(name).build();
    primitiveTypes.put(fullName, type);
    if (BOOLEAN.equals(type)) {
        return Types.primitive(PrimitiveType.PrimitiveTypeName.BOOLEAN, OPTIONAL).named(name);
    }
    if (INTEGER.equals(type) || SMALLINT.equals(type) || TINYINT.equals(type)) {
        return Types.primitive(PrimitiveType.PrimitiveTypeName.INT32, OPTIONAL).named(name);
    }
    if (type instanceof DecimalType) {
        DecimalType decimalType = (DecimalType) type;
        if (decimalType.getPrecision() <= 9) {
            return Types.optional(PrimitiveType.PrimitiveTypeName.INT32).as(OriginalType.DECIMAL).precision(decimalType.getPrecision()).scale(decimalType.getScale()).named(name);
        } else if (decimalType.isShort()) {
            return Types.optional(PrimitiveType.PrimitiveTypeName.INT64).as(OriginalType.DECIMAL).precision(decimalType.getPrecision()).scale(decimalType.getScale()).named(name);
        } else {
            return Types.optional(PrimitiveType.PrimitiveTypeName.FIXED_LEN_BYTE_ARRAY).length(16).as(OriginalType.DECIMAL).precision(decimalType.getPrecision()).scale(decimalType.getScale()).named(name);
        }
    }
    if (DATE.equals(type)) {
        return Types.optional(PrimitiveType.PrimitiveTypeName.INT32).as(OriginalType.DATE).named(name);
    }
    if (BIGINT.equals(type)) {
        return Types.primitive(PrimitiveType.PrimitiveTypeName.INT64, OPTIONAL).named(name);
    }
    if (type instanceof TimestampType) {
        TimestampType timestampType = (TimestampType) type;
        if (timestampType.getPrecision() <= 3) {
            return Types.primitive(PrimitiveType.PrimitiveTypeName.INT64, OPTIONAL).as(LogicalTypeAnnotation.timestampType(false, LogicalTypeAnnotation.TimeUnit.MILLIS)).named(name);
        }
        if (timestampType.getPrecision() <= 6) {
            return Types.primitive(PrimitiveType.PrimitiveTypeName.INT64, OPTIONAL).as(LogicalTypeAnnotation.timestampType(false, LogicalTypeAnnotation.TimeUnit.MICROS)).named(name);
        }
        if (timestampType.getPrecision() <= 9) {
            // even though it can only hold values within 1677-09-21 00:12:43 and 2262-04-11 23:47:16 range.
            return Types.primitive(PrimitiveType.PrimitiveTypeName.INT64, OPTIONAL).as(LogicalTypeAnnotation.timestampType(false, LogicalTypeAnnotation.TimeUnit.NANOS)).named(name);
        }
    }
    if (DOUBLE.equals(type)) {
        return Types.primitive(PrimitiveType.PrimitiveTypeName.DOUBLE, OPTIONAL).named(name);
    }
    if (RealType.REAL.equals(type)) {
        return Types.primitive(PrimitiveType.PrimitiveTypeName.FLOAT, OPTIONAL).named(name);
    }
    if (type instanceof VarcharType || type instanceof CharType) {
        return Types.primitive(PrimitiveType.PrimitiveTypeName.BINARY, OPTIONAL).as(LogicalTypeAnnotation.stringType()).named(name);
    }
    if (type instanceof VarbinaryType) {
        return Types.primitive(PrimitiveType.PrimitiveTypeName.BINARY, OPTIONAL).named(name);
    }
    throw new TrinoException(NOT_SUPPORTED, format("Unsupported primitive type: %s", type));
}
Also used : VarbinaryType(io.trino.spi.type.VarbinaryType) VarcharType(io.trino.spi.type.VarcharType) DecimalType(io.trino.spi.type.DecimalType) TimestampType(io.trino.spi.type.TimestampType) TrinoException(io.trino.spi.TrinoException) CharType(io.trino.spi.type.CharType)

Aggregations

VarbinaryType (io.trino.spi.type.VarbinaryType)8 VarcharType (io.trino.spi.type.VarcharType)8 CharType (io.trino.spi.type.CharType)7 DecimalType (io.trino.spi.type.DecimalType)7 TrinoException (io.trino.spi.TrinoException)5 DecimalType.createDecimalType (io.trino.spi.type.DecimalType.createDecimalType)5 ArrayType (io.trino.spi.type.ArrayType)4 VarcharType.createUnboundedVarcharType (io.trino.spi.type.VarcharType.createUnboundedVarcharType)4 TimestampType (io.trino.spi.type.TimestampType)3 Type (io.trino.spi.type.Type)3 MapType (io.trino.spi.type.MapType)2 RowType (io.trino.spi.type.RowType)2 TimeType (io.trino.spi.type.TimeType)2 JDBCType (java.sql.JDBCType)2 BloomType (org.apache.hadoop.hbase.regionserver.BloomType)2 PDataType (org.apache.phoenix.schema.types.PDataType)2 VerifyException (com.google.common.base.VerifyException)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImplementAvgFloatingPoint (io.trino.plugin.jdbc.aggregation.ImplementAvgFloatingPoint)1 SqlTime (io.trino.spi.type.SqlTime)1