Search in sources :

Example 61 with RowType

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

the class IcebergPageSourceProvider method getOrcReadType.

private static Type getOrcReadType(Type columnType, TypeManager typeManager) {
    if (columnType == UUID) {
        // TODO: Validate that the OrcColumn attribute ICEBERG_BINARY_TYPE is equal to "UUID"
        return VARBINARY;
    }
    if (columnType instanceof ArrayType) {
        return new ArrayType(getOrcReadType(((ArrayType) columnType).getElementType(), typeManager));
    }
    if (columnType instanceof MapType) {
        MapType mapType = (MapType) columnType;
        Type keyType = getOrcReadType(mapType.getKeyType(), typeManager);
        Type valueType = getOrcReadType(mapType.getValueType(), typeManager);
        return new MapType(keyType, valueType, typeManager.getTypeOperators());
    }
    if (columnType instanceof RowType) {
        return RowType.from(((RowType) columnType).getFields().stream().map(field -> new RowType.Field(field.getName(), getOrcReadType(field.getType(), typeManager))).collect(toImmutableList()));
    }
    return columnType;
}
Also used : ArrayType(io.trino.spi.type.ArrayType) OrcType(io.trino.orc.metadata.OrcType) MapType(io.trino.spi.type.MapType) RowType(io.trino.spi.type.RowType) ArrayType(io.trino.spi.type.ArrayType) MessageType(org.apache.parquet.schema.MessageType) Type(io.trino.spi.type.Type) RowType(io.trino.spi.type.RowType) MapType(io.trino.spi.type.MapType)

Example 62 with RowType

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

the class TestHiveBucketing method toNativeContainerValue.

private static Object toNativeContainerValue(Type type, Object hiveValue) {
    if (hiveValue == null) {
        return null;
    }
    if (type instanceof ArrayType) {
        BlockBuilder blockBuilder = type.createBlockBuilder(null, 1);
        BlockBuilder subBlockBuilder = blockBuilder.beginBlockEntry();
        for (Object subElement : (Iterable<?>) hiveValue) {
            appendToBlockBuilder(type.getTypeParameters().get(0), subElement, subBlockBuilder);
        }
        blockBuilder.closeEntry();
        return type.getObject(blockBuilder, 0);
    }
    if (type instanceof RowType) {
        BlockBuilder blockBuilder = type.createBlockBuilder(null, 1);
        BlockBuilder subBlockBuilder = blockBuilder.beginBlockEntry();
        int field = 0;
        for (Object subElement : (Iterable<?>) hiveValue) {
            appendToBlockBuilder(type.getTypeParameters().get(field), subElement, subBlockBuilder);
            field++;
        }
        blockBuilder.closeEntry();
        return type.getObject(blockBuilder, 0);
    }
    if (type instanceof MapType) {
        BlockBuilder blockBuilder = type.createBlockBuilder(null, 1);
        BlockBuilder subBlockBuilder = blockBuilder.beginBlockEntry();
        for (Entry<?, ?> entry : ((Map<?, ?>) hiveValue).entrySet()) {
            appendToBlockBuilder(type.getTypeParameters().get(0), entry.getKey(), subBlockBuilder);
            appendToBlockBuilder(type.getTypeParameters().get(1), entry.getValue(), subBlockBuilder);
        }
        blockBuilder.closeEntry();
        return type.getObject(blockBuilder, 0);
    }
    if (type instanceof BooleanType) {
        return hiveValue;
    }
    if (type instanceof TinyintType) {
        return (long) (byte) hiveValue;
    }
    if (type instanceof SmallintType) {
        return (long) (short) hiveValue;
    }
    if (type instanceof IntegerType) {
        return (long) (int) hiveValue;
    }
    if (type instanceof BigintType) {
        return hiveValue;
    }
    if (type instanceof RealType) {
        return (long) Float.floatToRawIntBits((float) hiveValue);
    }
    if (type instanceof DoubleType) {
        return hiveValue;
    }
    if (type instanceof VarcharType) {
        return Slices.utf8Slice(hiveValue.toString());
    }
    if (type instanceof DateType) {
        return (long) ((Date) hiveValue).toEpochDay();
    }
    throw new IllegalArgumentException("Unsupported bucketing type: " + type);
}
Also used : VarcharType(io.trino.spi.type.VarcharType) TinyintType(io.trino.spi.type.TinyintType) BooleanType(io.trino.spi.type.BooleanType) RowType(io.trino.spi.type.RowType) RealType(io.trino.spi.type.RealType) MapType(io.trino.spi.type.MapType) BigintType(io.trino.spi.type.BigintType) ArrayType(io.trino.spi.type.ArrayType) IntegerType(io.trino.spi.type.IntegerType) DoubleType(io.trino.spi.type.DoubleType) SmallintType(io.trino.spi.type.SmallintType) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) DateType(io.trino.spi.type.DateType) BlockBuilder(io.trino.spi.block.BlockBuilder)

Example 63 with RowType

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

the class TestSerDeUtils method testStructBlock.

@Test
public void testStructBlock() {
    // test simple structs
    InnerStruct innerStruct = new InnerStruct(13, 14L);
    io.trino.spi.type.Type rowType = RowType.anonymous(ImmutableList.of(INTEGER, BIGINT));
    Block actual = toBinaryBlock(rowType, innerStruct, getInspector(InnerStruct.class));
    Block expected = rowBlockOf(ImmutableList.of(INTEGER, BIGINT), 13, 14L);
    assertBlockEquals(actual, expected);
    // test complex structs
    OuterStruct outerStruct = new OuterStruct();
    outerStruct.byteVal = (byte) 1;
    outerStruct.shortVal = (short) 2;
    outerStruct.intVal = 3;
    outerStruct.longVal = 4L;
    outerStruct.floatVal = 5.01f;
    outerStruct.doubleVal = 6.001d;
    outerStruct.stringVal = "seven";
    outerStruct.byteArray = new byte[] { '2' };
    InnerStruct is1 = new InnerStruct(2, -5L);
    InnerStruct is2 = new InnerStruct(-10, 0L);
    outerStruct.structArray = new ArrayList<>(2);
    outerStruct.structArray.add(is1);
    outerStruct.structArray.add(is2);
    outerStruct.map = new TreeMap<>();
    outerStruct.map.put("twelve", new InnerStruct(0, 5L));
    outerStruct.map.put("fifteen", new InnerStruct(-5, -10L));
    outerStruct.innerStruct = new InnerStruct(18, 19L);
    io.trino.spi.type.Type innerRowType = RowType.anonymous(ImmutableList.of(INTEGER, BIGINT));
    io.trino.spi.type.Type arrayOfInnerRowType = new ArrayType(innerRowType);
    io.trino.spi.type.Type mapOfInnerRowType = mapType(createUnboundedVarcharType(), innerRowType);
    List<io.trino.spi.type.Type> outerRowParameterTypes = ImmutableList.of(TINYINT, SMALLINT, INTEGER, BIGINT, REAL, DOUBLE, createUnboundedVarcharType(), createUnboundedVarcharType(), arrayOfInnerRowType, mapOfInnerRowType, innerRowType);
    io.trino.spi.type.Type outerRowType = RowType.anonymous(outerRowParameterTypes);
    actual = toBinaryBlock(outerRowType, outerStruct, getInspector(OuterStruct.class));
    ImmutableList.Builder<Object> outerRowValues = ImmutableList.builder();
    outerRowValues.add((byte) 1);
    outerRowValues.add((short) 2);
    outerRowValues.add(3);
    outerRowValues.add(4L);
    outerRowValues.add(5.01f);
    outerRowValues.add(6.001d);
    outerRowValues.add("seven");
    outerRowValues.add(new byte[] { '2' });
    outerRowValues.add(arrayBlockOf(innerRowType, rowBlockOf(innerRowType.getTypeParameters(), 2, -5L), rowBlockOf(ImmutableList.of(INTEGER, BIGINT), -10, 0L)));
    outerRowValues.add(mapBlockOf(VARCHAR, innerRowType, new Object[] { utf8Slice("fifteen"), utf8Slice("twelve") }, new Object[] { rowBlockOf(innerRowType.getTypeParameters(), -5, -10L), rowBlockOf(innerRowType.getTypeParameters(), 0, 5L) }));
    outerRowValues.add(rowBlockOf(ImmutableList.of(INTEGER, BIGINT), 18, 19L));
    assertBlockEquals(actual, rowBlockOf(outerRowParameterTypes, outerRowValues.build().toArray()));
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) ArrayType(io.trino.spi.type.ArrayType) RowType(io.trino.spi.type.RowType) ArrayType(io.trino.spi.type.ArrayType) Type(java.lang.reflect.Type) VarcharType.createUnboundedVarcharType(io.trino.spi.type.VarcharType.createUnboundedVarcharType) HiveTestUtils.mapType(io.trino.plugin.hive.HiveTestUtils.mapType) Block(io.trino.spi.block.Block) SerDeUtils.serializeObject(io.trino.plugin.hive.util.SerDeUtils.serializeObject) SerDeUtils.getBlockObject(io.trino.plugin.hive.util.SerDeUtils.getBlockObject) Test(org.testng.annotations.Test)

Example 64 with RowType

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

the class ValuesStatsRule method getSymbolValues.

private List<Object> getSymbolValues(ValuesNode valuesNode, int symbolId, Session session, Type rowType) {
    Type symbolType = rowType.getTypeParameters().get(symbolId);
    if (UNKNOWN.equals(symbolType)) {
        // special casing for UNKNOWN as evaluateConstantExpression does not handle that
        return IntStream.range(0, valuesNode.getRowCount()).mapToObj(rowId -> null).collect(toList());
    }
    checkState(valuesNode.getRows().isPresent(), "rows is empty");
    return valuesNode.getRows().get().stream().map(row -> {
        Object rowValue = evaluateConstantExpression(row, rowType, plannerContext, session, new AllowAllAccessControl(), ImmutableMap.of());
        return readNativeValue(symbolType, (SingleRowBlock) rowValue, symbolId);
    }).collect(toList());
}
Also used : IntStream(java.util.stream.IntStream) AllowAllAccessControl(io.trino.security.AllowAllAccessControl) ExpressionInterpreter.evaluateConstantExpression(io.trino.sql.planner.ExpressionInterpreter.evaluateConstantExpression) Patterns.values(io.trino.sql.planner.plan.Patterns.values) Type(io.trino.spi.type.Type) OptionalDouble(java.util.OptionalDouble) UNKNOWN(io.trino.type.UnknownType.UNKNOWN) StatsUtil.toStatsRepresentation(io.trino.spi.statistics.StatsUtil.toStatsRepresentation) Objects.requireNonNull(java.util.Objects.requireNonNull) Symbol(io.trino.sql.planner.Symbol) RowType(io.trino.spi.type.RowType) ImmutableMap(com.google.common.collect.ImmutableMap) Lookup(io.trino.sql.planner.iterative.Lookup) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) SingleRowBlock(io.trino.spi.block.SingleRowBlock) DoubleStream(java.util.stream.DoubleStream) Preconditions.checkState(com.google.common.base.Preconditions.checkState) Objects(java.util.Objects) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Rule(io.trino.cost.ComposableStatsCalculator.Rule) Pattern(io.trino.matching.Pattern) TypeProvider(io.trino.sql.planner.TypeProvider) Optional(java.util.Optional) ValuesNode(io.trino.sql.planner.plan.ValuesNode) TypeUtils.readNativeValue(io.trino.spi.type.TypeUtils.readNativeValue) Session(io.trino.Session) PlannerContext(io.trino.sql.PlannerContext) Type(io.trino.spi.type.Type) RowType(io.trino.spi.type.RowType) AllowAllAccessControl(io.trino.security.AllowAllAccessControl) SingleRowBlock(io.trino.spi.block.SingleRowBlock)

Example 65 with RowType

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

the class OperatorAssertion method toRow.

public static Block toRow(List<Type> parameterTypes, Object... values) {
    checkArgument(parameterTypes.size() == values.length, "parameterTypes.size(" + parameterTypes.size() + ") does not equal to values.length(" + values.length + ")");
    RowType rowType = RowType.anonymous(parameterTypes);
    BlockBuilder blockBuilder = new RowBlockBuilder(parameterTypes, null, 1);
    BlockBuilder singleRowBlockWriter = blockBuilder.beginBlockEntry();
    for (int i = 0; i < values.length; i++) {
        appendToBlockBuilder(parameterTypes.get(i), values[i], singleRowBlockWriter);
    }
    blockBuilder.closeEntry();
    return rowType.getObject(blockBuilder, 0);
}
Also used : RowBlockBuilder(io.trino.spi.block.RowBlockBuilder) RowType(io.trino.spi.type.RowType) StructuralTestUtil.appendToBlockBuilder(io.trino.util.StructuralTestUtil.appendToBlockBuilder) BlockBuilder(io.trino.spi.block.BlockBuilder) RowBlockBuilder(io.trino.spi.block.RowBlockBuilder)

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