Search in sources :

Example 36 with RowType

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

the class CheckpointEntryIterator method readMinMax.

private Map<String, Object> readMinMax(Block block, int blockPosition, List<ColumnMetadata> eligibleColumns) {
    if (block.isNull(blockPosition)) {
        // Statistics were not collected
        return ImmutableMap.of();
    }
    Block valuesBlock = block.getObject(blockPosition, Block.class);
    ImmutableMap.Builder<String, Object> values = ImmutableMap.builder();
    for (int i = 0; i < eligibleColumns.size(); i++) {
        ColumnMetadata metadata = eligibleColumns.get(i);
        String name = metadata.getName();
        Type type = metadata.getType();
        if (valuesBlock.isNull(i)) {
            continue;
        }
        if (type instanceof RowType) {
            if (checkpointRowStatisticsWritingEnabled) {
                // RowType column statistics are not used for query planning, but need to be copied when writing out new Checkpoint files.
                values.put(name, valuesBlock.getSingleValueBlock(i));
            }
            continue;
        }
        if (type instanceof TimestampWithTimeZoneType) {
            Instant instant = Instant.ofEpochMilli(LongMath.divide((Long) readNativeValue(TIMESTAMP_MILLIS, valuesBlock, i), MICROSECONDS_PER_MILLISECOND, UNNECESSARY));
            if (!instant.atZone(UTC).toLocalDate().isBefore(START_OF_MODERN_ERA)) {
                values.put(name, packDateTimeWithZone(instant.toEpochMilli(), UTC_KEY));
            }
            continue;
        }
        values.put(name, readNativeValue(type, valuesBlock, i));
    }
    return values.buildOrThrow();
}
Also used : ColumnMetadata(io.trino.spi.connector.ColumnMetadata) TimestampWithTimeZoneType(io.trino.spi.type.TimestampWithTimeZoneType) RowType(io.trino.spi.type.RowType) ArrayType(io.trino.spi.type.ArrayType) Type(io.trino.spi.type.Type) VarcharType(io.trino.spi.type.VarcharType) MapType(io.trino.spi.type.MapType) Instant(java.time.Instant) TimestampWithTimeZoneType(io.trino.spi.type.TimestampWithTimeZoneType) Block(io.trino.spi.block.Block) RowType(io.trino.spi.type.RowType) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 37 with RowType

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

the class BlockAssertions method createRandomBlockForNestedType.

private static Block createRandomBlockForNestedType(Type type, int positionCount, float nullRate) {
    // Builds isNull and offsets of size positionCount
    boolean[] isNull = null;
    Set<Integer> nullPositions = null;
    if (nullRate > 0) {
        isNull = new boolean[positionCount];
        nullPositions = chooseNullPositions(positionCount, nullRate);
    }
    int[] offsets = new int[positionCount + 1];
    for (int position = 0; position < positionCount; position++) {
        if (nullRate > 0 && nullPositions.contains(position)) {
            isNull[position] = true;
            offsets[position + 1] = offsets[position];
        } else {
            // RowType doesn't need offsets, so we just use 1,
            // for ArrayType and MapType we choose randomly either array length or map size at the current position
            offsets[position + 1] = offsets[position] + (type instanceof RowType ? 1 : RANDOM.nextInt(ENTRY_SIZE) + 1);
        }
    }
    // Builds the nested block of size offsets[positionCount].
    if (type instanceof ArrayType) {
        Block valuesBlock = createRandomBlockForType(((ArrayType) type).getElementType(), offsets[positionCount], nullRate);
        return fromElementBlock(positionCount, Optional.ofNullable(isNull), offsets, valuesBlock);
    }
    if (type instanceof MapType) {
        MapType mapType = (MapType) type;
        Block keyBlock = createRandomBlockForType(mapType.getKeyType(), offsets[positionCount], 0.0f);
        Block valueBlock = createRandomBlockForType(mapType.getValueType(), offsets[positionCount], nullRate);
        return mapType.createBlockFromKeyValue(Optional.ofNullable(isNull), offsets, keyBlock, valueBlock);
    }
    if (type instanceof RowType) {
        List<Type> fieldTypes = type.getTypeParameters();
        Block[] fieldBlocks = new Block[fieldTypes.size()];
        for (int i = 0; i < fieldBlocks.length; i++) {
            fieldBlocks[i] = createRandomBlockForType(fieldTypes.get(i), positionCount, nullRate);
        }
        return RowBlock.fromFieldBlocks(positionCount, Optional.ofNullable(isNull), fieldBlocks);
    }
    throw new IllegalArgumentException(format("type %s is not supported.", type));
}
Also used : RowType(io.trino.spi.type.RowType) MapType(io.trino.spi.type.MapType) BigInteger(java.math.BigInteger) ArrayType(io.trino.spi.type.ArrayType) RowType(io.trino.spi.type.RowType) ArrayType(io.trino.spi.type.ArrayType) DecimalType(io.trino.spi.type.DecimalType) Type(io.trino.spi.type.Type) TimestampType(io.trino.spi.type.TimestampType) MapType(io.trino.spi.type.MapType) CharType(io.trino.spi.type.CharType) Block(io.trino.spi.block.Block) RunLengthEncodedBlock(io.trino.spi.block.RunLengthEncodedBlock) ArrayBlock.fromElementBlock(io.trino.spi.block.ArrayBlock.fromElementBlock) DictionaryBlock(io.trino.spi.block.DictionaryBlock) RowBlock(io.trino.spi.block.RowBlock)

Example 38 with RowType

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

the class OrcType method toOrcType.

private static List<OrcType> toOrcType(int nextFieldTypeIndex, Type type) {
    if (BOOLEAN.equals(type)) {
        return ImmutableList.of(new OrcType(OrcTypeKind.BOOLEAN));
    }
    if (TINYINT.equals(type)) {
        return ImmutableList.of(new OrcType(OrcTypeKind.BYTE));
    }
    if (SMALLINT.equals(type)) {
        return ImmutableList.of(new OrcType(OrcTypeKind.SHORT));
    }
    if (INTEGER.equals(type)) {
        return ImmutableList.of(new OrcType(OrcTypeKind.INT));
    }
    if (BIGINT.equals(type)) {
        return ImmutableList.of(new OrcType(OrcTypeKind.LONG));
    }
    if (DOUBLE.equals(type)) {
        return ImmutableList.of(new OrcType(OrcTypeKind.DOUBLE));
    }
    if (REAL.equals(type)) {
        return ImmutableList.of(new OrcType(OrcTypeKind.FLOAT));
    }
    if (type instanceof VarcharType) {
        VarcharType varcharType = (VarcharType) type;
        if (varcharType.isUnbounded()) {
            return ImmutableList.of(new OrcType(OrcTypeKind.STRING));
        }
        return ImmutableList.of(new OrcType(OrcTypeKind.VARCHAR, varcharType.getBoundedLength()));
    }
    if (type instanceof CharType) {
        return ImmutableList.of(new OrcType(OrcTypeKind.CHAR, ((CharType) type).getLength()));
    }
    if (VARBINARY.equals(type)) {
        return ImmutableList.of(new OrcType(OrcTypeKind.BINARY));
    }
    if (DATE.equals(type)) {
        return ImmutableList.of(new OrcType(OrcTypeKind.DATE));
    }
    if (TIMESTAMP_MILLIS.equals(type) || TIMESTAMP_MICROS.equals(type) || TIMESTAMP_NANOS.equals(type)) {
        return ImmutableList.of(new OrcType(OrcTypeKind.TIMESTAMP));
    }
    if (TIMESTAMP_TZ_MILLIS.equals(type) || TIMESTAMP_TZ_MICROS.equals(type) || TIMESTAMP_TZ_NANOS.equals(type)) {
        return ImmutableList.of(new OrcType(OrcTypeKind.TIMESTAMP_INSTANT));
    }
    if (type instanceof DecimalType) {
        DecimalType decimalType = (DecimalType) type;
        return ImmutableList.of(new OrcType(OrcTypeKind.DECIMAL, decimalType.getPrecision(), decimalType.getScale()));
    }
    if (type instanceof ArrayType) {
        return createOrcArrayType(nextFieldTypeIndex, type.getTypeParameters().get(0));
    }
    if (type instanceof MapType) {
        return createOrcMapType(nextFieldTypeIndex, type.getTypeParameters().get(0), type.getTypeParameters().get(1));
    }
    if (type instanceof RowType) {
        List<String> fieldNames = new ArrayList<>();
        for (int i = 0; i < type.getTypeSignature().getParameters().size(); i++) {
            TypeSignatureParameter parameter = type.getTypeSignature().getParameters().get(i);
            fieldNames.add(parameter.getNamedTypeSignature().getName().orElse("field" + i));
        }
        List<Type> fieldTypes = type.getTypeParameters();
        return createOrcRowType(nextFieldTypeIndex, fieldNames, fieldTypes);
    }
    throw new TrinoException(NOT_SUPPORTED, format("Unsupported Hive type: %s", type));
}
Also used : VarcharType(io.trino.spi.type.VarcharType) ArrayList(java.util.ArrayList) RowType(io.trino.spi.type.RowType) MapType(io.trino.spi.type.MapType) ArrayType(io.trino.spi.type.ArrayType) Type(io.trino.spi.type.Type) VarcharType(io.trino.spi.type.VarcharType) RowType(io.trino.spi.type.RowType) MapType(io.trino.spi.type.MapType) ArrayType(io.trino.spi.type.ArrayType) CharType(io.trino.spi.type.CharType) DecimalType(io.trino.spi.type.DecimalType) TypeSignatureParameter(io.trino.spi.type.TypeSignatureParameter) DecimalType(io.trino.spi.type.DecimalType) TrinoException(io.trino.spi.TrinoException) CharType(io.trino.spi.type.CharType)

Example 39 with RowType

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

the class BigQueryResultPageSource method writeBlock.

private void writeBlock(BlockBuilder output, Type type, Object value) {
    if (type instanceof ArrayType && value instanceof List<?>) {
        BlockBuilder builder = output.beginBlockEntry();
        for (Object element : (List<?>) value) {
            appendTo(type.getTypeParameters().get(0), element, builder);
        }
        output.closeEntry();
        return;
    }
    if (type instanceof RowType && value instanceof GenericRecord) {
        GenericRecord record = (GenericRecord) value;
        BlockBuilder builder = output.beginBlockEntry();
        List<String> fieldNames = new ArrayList<>();
        for (int i = 0; i < type.getTypeSignature().getParameters().size(); i++) {
            TypeSignatureParameter parameter = type.getTypeSignature().getParameters().get(i);
            fieldNames.add(parameter.getNamedTypeSignature().getName().orElse("field" + i));
        }
        checkState(fieldNames.size() == type.getTypeParameters().size(), "fieldName doesn't match with type size : %s", type);
        for (int index = 0; index < type.getTypeParameters().size(); index++) {
            appendTo(type.getTypeParameters().get(index), record.get(fieldNames.get(index)), builder);
        }
        output.closeEntry();
        return;
    }
    throw new TrinoException(GENERIC_INTERNAL_ERROR, "Unhandled type for Block: " + type.getTypeSignature());
}
Also used : ArrayList(java.util.ArrayList) RowType(io.trino.spi.type.RowType) ArrayType(io.trino.spi.type.ArrayType) TypeSignatureParameter(io.trino.spi.type.TypeSignatureParameter) TrinoException(io.trino.spi.TrinoException) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) List(java.util.List) ArrayList(java.util.ArrayList) GenericRecord(org.apache.avro.generic.GenericRecord) BlockBuilder(io.trino.spi.block.BlockBuilder)

Example 40 with RowType

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

the class TestTypeConversions method testConvertOneLevelRecordColumn.

@Test
public void testConvertOneLevelRecordColumn() {
    BigQueryColumnHandle column = new BigQueryColumnHandle("rec", BigQueryType.RECORD, Field.Mode.NULLABLE, null, null, ImmutableList.of(new BigQueryColumnHandle("sub_s", BigQueryType.STRING, Field.Mode.NULLABLE, null, null, ImmutableList.of(), null), new BigQueryColumnHandle("sub_i", BigQueryType.INTEGER, Field.Mode.NULLABLE, null, null, ImmutableList.of(), null)), null);
    ColumnMetadata metadata = column.getColumnMetadata();
    RowType targetType = RowType.rowType(RowType.field("sub_s", VarcharType.VARCHAR), RowType.field("sub_i", BigintType.BIGINT));
    assertThat(metadata.getType()).isEqualTo(targetType);
}
Also used : ColumnMetadata(io.trino.spi.connector.ColumnMetadata) RowType(io.trino.spi.type.RowType) Test(org.testng.annotations.Test)

Aggregations

RowType (io.trino.spi.type.RowType)90 ArrayType (io.trino.spi.type.ArrayType)51 MapType (io.trino.spi.type.MapType)42 Type (io.trino.spi.type.Type)42 ImmutableList (com.google.common.collect.ImmutableList)30 VarcharType (io.trino.spi.type.VarcharType)28 BlockBuilder (io.trino.spi.block.BlockBuilder)26 DecimalType (io.trino.spi.type.DecimalType)21 Test (org.testng.annotations.Test)21 ImmutableMap (com.google.common.collect.ImmutableMap)20 Block (io.trino.spi.block.Block)20 List (java.util.List)20 Map (java.util.Map)18 ArrayList (java.util.ArrayList)17 Optional (java.util.Optional)17 CharType (io.trino.spi.type.CharType)16 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)15 ColumnMetadata (io.trino.spi.connector.ColumnMetadata)12 TimestampWithTimeZoneType (io.trino.spi.type.TimestampWithTimeZoneType)12 HashMap (java.util.HashMap)12