Search in sources :

Example 61 with VarcharType

use of io.trino.spi.type.VarcharType 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)

Example 62 with VarcharType

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

the class ImplementCountDistinct method rewrite.

@Override
public Optional<JdbcExpression> rewrite(AggregateFunction aggregateFunction, Captures captures, RewriteContext<String> context) {
    Variable argument = captures.get(ARGUMENT);
    JdbcColumnHandle columnHandle = (JdbcColumnHandle) context.getAssignment(argument.getName());
    verify(aggregateFunction.getOutputType() == BIGINT);
    boolean isCaseSensitiveType = columnHandle.getColumnType() instanceof CharType || columnHandle.getColumnType() instanceof VarcharType;
    if (aggregateFunction.isDistinct() && !isRemoteCollationSensitive && isCaseSensitiveType) {
        // Remote database is case insensitive or compares values differently from Trino
        return Optional.empty();
    }
    return Optional.of(new JdbcExpression(format("count(DISTINCT %s)", context.rewriteExpression(argument).orElseThrow()), bigintTypeHandle));
}
Also used : Variable(io.trino.spi.expression.Variable) VarcharType(io.trino.spi.type.VarcharType) JdbcColumnHandle(io.trino.plugin.jdbc.JdbcColumnHandle) JdbcExpression(io.trino.plugin.jdbc.JdbcExpression) CharType(io.trino.spi.type.CharType)

Example 63 with VarcharType

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

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, (Long) value);
    } else if (INTEGER.equals(type)) {
        type.writeLong(blockBuilder, (Integer) value);
    } else if (SMALLINT.equals(type)) {
        type.writeLong(blockBuilder, (Short) value);
    } else if (TINYINT.equals(type)) {
        type.writeLong(blockBuilder, (Byte) value);
    } else if (REAL.equals(type)) {
        type.writeLong(blockBuilder, floatToRawIntBits(((Float) value)));
    } else if (DOUBLE.equals(type)) {
        type.writeDouble(blockBuilder, (Double) value);
    } else if (BOOLEAN.equals(type)) {
        type.writeBoolean(blockBuilder, (Boolean) value);
    } else if (JSON.equals(type)) {
        type.writeSlice(blockBuilder, Slices.utf8Slice((String) 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 (type instanceof TimeType) {
        SqlTime time = (SqlTime) value;
        type.writeLong(blockBuilder, time.getPicos());
    } else if (type instanceof TimeWithTimeZoneType) {
        long nanos = roundDiv(((SqlTimeWithTimeZone) value).getPicos(), PICOSECONDS_PER_NANOSECOND);
        int offsetMinutes = ((SqlTimeWithTimeZone) value).getOffsetMinutes();
        type.writeLong(blockBuilder, packTimeWithTimeZone(nanos, offsetMinutes));
    } else if (type instanceof TimestampType) {
        long micros = ((SqlTimestamp) value).getEpochMicros();
        if (((TimestampType) type).getPrecision() <= TimestampType.MAX_SHORT_PRECISION) {
            type.writeLong(blockBuilder, micros);
        } else {
            type.writeObject(blockBuilder, new LongTimestamp(micros, ((SqlTimestamp) value).getPicosOfMicros()));
        }
    } 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 (type instanceof ArrayType) {
        List<?> list = (List<?>) value;
        Type elementType = ((ArrayType) type).getElementType();
        BlockBuilder arrayBlockBuilder = blockBuilder.beginBlockEntry();
        for (Object element : list) {
            writeValue(elementType, arrayBlockBuilder, element);
        }
        blockBuilder.closeEntry();
    } else if (type instanceof MapType) {
        Map<?, ?> map = (Map<?, ?>) value;
        Type keyType = ((MapType) type).getKeyType();
        Type valueType = ((MapType) type).getValueType();
        BlockBuilder mapBlockBuilder = blockBuilder.beginBlockEntry();
        for (Entry<?, ?> entry : map.entrySet()) {
            writeValue(keyType, mapBlockBuilder, entry.getKey());
            writeValue(valueType, mapBlockBuilder, entry.getValue());
        }
        blockBuilder.closeEntry();
    } else if (type instanceof RowType) {
        List<?> row = (List<?>) 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(io.trino.spi.type.VarcharType) SqlTime(io.trino.spi.type.SqlTime) TimeWithTimeZoneType(io.trino.spi.type.TimeWithTimeZoneType) RowType(io.trino.spi.type.RowType) MapType(io.trino.spi.type.MapType) TimeType(io.trino.spi.type.TimeType) ArrayType(io.trino.spi.type.ArrayType) TimestampType(io.trino.spi.type.TimestampType) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) TimeZoneKey(io.trino.spi.type.TimeZoneKey) BlockBuilder(io.trino.spi.block.BlockBuilder) LongTimestamp(io.trino.spi.type.LongTimestamp) SqlTimeWithTimeZone(io.trino.spi.type.SqlTimeWithTimeZone) RowType(io.trino.spi.type.RowType) ArrayType(io.trino.spi.type.ArrayType) TimeWithTimeZoneType(io.trino.spi.type.TimeWithTimeZoneType) TimeType(io.trino.spi.type.TimeType) Type(io.trino.spi.type.Type) TimestampType(io.trino.spi.type.TimestampType) VarcharType(io.trino.spi.type.VarcharType) MapType(io.trino.spi.type.MapType) CharType(io.trino.spi.type.CharType) SqlTimestampWithTimeZone(io.trino.spi.type.SqlTimestampWithTimeZone) SqlDate(io.trino.spi.type.SqlDate) OptionalLong(java.util.OptionalLong) CharType(io.trino.spi.type.CharType) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 64 with VarcharType

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

the class TestValueSet method testRejectNullOnCreate.

@Test
public void testRejectNullOnCreate() {
    // Using varchar for the test since its stack representation is not primitive, so a null is more likely to go unnoticed.
    VarcharType varcharType = createVarcharType(25);
    Slice abc = utf8Slice("abc");
    Slice def = utf8Slice("def");
    Slice ghi = utf8Slice("ghi");
    assertThatThrownBy(() -> ValueSet.of(varcharType, null)).hasMessage("value is null");
    assertThatThrownBy(() -> ValueSet.of(varcharType, abc, def, null, ghi)).hasMessage("value is null");
    assertThatThrownBy(() -> ValueSet.copyOf(varcharType, asList(abc, def, null, ghi))).hasMessage("value is null");
}
Also used : VarcharType(io.trino.spi.type.VarcharType) VarcharType.createVarcharType(io.trino.spi.type.VarcharType.createVarcharType) Slice(io.airlift.slice.Slice) Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) Test(org.testng.annotations.Test)

Example 65 with VarcharType

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

the class AbstractTestHive method assertValueTypes.

private static void assertValueTypes(MaterializedRow row, List<ColumnMetadata> schema) {
    for (int columnIndex = 0; columnIndex < schema.size(); columnIndex++) {
        ColumnMetadata column = schema.get(columnIndex);
        Object value = row.getField(columnIndex);
        if (value != null) {
            if (BOOLEAN.equals(column.getType())) {
                assertInstanceOf(value, Boolean.class);
            } else if (TINYINT.equals(column.getType())) {
                assertInstanceOf(value, Byte.class);
            } else if (SMALLINT.equals(column.getType())) {
                assertInstanceOf(value, Short.class);
            } else if (INTEGER.equals(column.getType())) {
                assertInstanceOf(value, Integer.class);
            } else if (BIGINT.equals(column.getType())) {
                assertInstanceOf(value, Long.class);
            } else if (DOUBLE.equals(column.getType())) {
                assertInstanceOf(value, Double.class);
            } else if (REAL.equals(column.getType())) {
                assertInstanceOf(value, Float.class);
            } else if (column.getType() instanceof VarcharType) {
                assertInstanceOf(value, String.class);
            } else if (column.getType() instanceof CharType) {
                assertInstanceOf(value, String.class);
            } else if (VARBINARY.equals(column.getType())) {
                assertInstanceOf(value, SqlVarbinary.class);
            } else if (TIMESTAMP_MILLIS.equals(column.getType())) {
                assertInstanceOf(value, SqlTimestamp.class);
            } else if (TIMESTAMP_WITH_TIME_ZONE.equals(column.getType())) {
                assertInstanceOf(value, SqlTimestampWithTimeZone.class);
            } else if (DATE.equals(column.getType())) {
                assertInstanceOf(value, SqlDate.class);
            } else if (column.getType() instanceof ArrayType || column.getType() instanceof RowType) {
                assertInstanceOf(value, List.class);
            } else if (column.getType() instanceof MapType) {
                assertInstanceOf(value, Map.class);
            } else {
                fail("Unknown primitive type " + columnIndex);
            }
        }
    }
}
Also used : ColumnMetadata(io.trino.spi.connector.ColumnMetadata) VarcharType.createVarcharType(io.trino.spi.type.VarcharType.createVarcharType) VarcharType.createUnboundedVarcharType(io.trino.spi.type.VarcharType.createUnboundedVarcharType) VarcharType(io.trino.spi.type.VarcharType) SqlVarbinary(io.trino.spi.type.SqlVarbinary) RowType(io.trino.spi.type.RowType) OptionalDouble(java.util.OptionalDouble) Constraint(io.trino.spi.connector.Constraint) MapType(io.trino.spi.type.MapType) ArrayType(io.trino.spi.type.ArrayType) SqlTimestampWithTimeZone(io.trino.spi.type.SqlTimestampWithTimeZone) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) ImmutableList(com.google.common.collect.ImmutableList) Collectors.toList(java.util.stream.Collectors.toList) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) List(java.util.List) CharType.createCharType(io.trino.spi.type.CharType.createCharType) CharType(io.trino.spi.type.CharType)

Aggregations

VarcharType (io.trino.spi.type.VarcharType)83 DecimalType (io.trino.spi.type.DecimalType)50 CharType (io.trino.spi.type.CharType)47 Type (io.trino.spi.type.Type)37 TrinoException (io.trino.spi.TrinoException)35 ArrayType (io.trino.spi.type.ArrayType)29 TimestampType (io.trino.spi.type.TimestampType)23 VarcharType.createUnboundedVarcharType (io.trino.spi.type.VarcharType.createUnboundedVarcharType)23 MapType (io.trino.spi.type.MapType)21 Slice (io.airlift.slice.Slice)20 DecimalType.createDecimalType (io.trino.spi.type.DecimalType.createDecimalType)18 RowType (io.trino.spi.type.RowType)18 ImmutableList (com.google.common.collect.ImmutableList)17 TimeType (io.trino.spi.type.TimeType)15 VarbinaryType (io.trino.spi.type.VarbinaryType)15 Block (io.trino.spi.block.Block)14 TimestampWithTimeZoneType (io.trino.spi.type.TimestampWithTimeZoneType)14 BigDecimal (java.math.BigDecimal)14 List (java.util.List)14 Slices.utf8Slice (io.airlift.slice.Slices.utf8Slice)12