Search in sources :

Example 86 with RowType

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

the class TestPushLimitThroughProject method testDoesntPushDownLimitThroughExclusiveDereferences.

@Test
public void testDoesntPushDownLimitThroughExclusiveDereferences() {
    RowType rowType = RowType.from(ImmutableList.of(new RowType.Field(Optional.of("x"), BIGINT), new RowType.Field(Optional.of("y"), BIGINT)));
    tester().assertThat(new PushLimitThroughProject(tester().getTypeAnalyzer())).on(p -> {
        Symbol a = p.symbol("a", rowType);
        return p.limit(1, p.project(Assignments.of(p.symbol("b"), new SubscriptExpression(a.toSymbolReference(), new LongLiteral("1")), p.symbol("c"), new SubscriptExpression(a.toSymbolReference(), new LongLiteral("2"))), p.values(a)));
    }).doesNotFire();
}
Also used : ExpressionMatcher(io.trino.sql.planner.assertions.ExpressionMatcher) Symbol(io.trino.sql.planner.Symbol) PlanMatchPattern.expression(io.trino.sql.planner.assertions.PlanMatchPattern.expression) RowType(io.trino.spi.type.RowType) ImmutableMap(com.google.common.collect.ImmutableMap) BaseRuleTest(io.trino.sql.planner.iterative.rule.test.BaseRuleTest) Assignments(io.trino.sql.planner.plan.Assignments) Test(org.testng.annotations.Test) PlanMatchPattern.values(io.trino.sql.planner.assertions.PlanMatchPattern.values) TRUE_LITERAL(io.trino.sql.tree.BooleanLiteral.TRUE_LITERAL) FIRST(io.trino.sql.tree.SortItem.NullOrdering.FIRST) SubscriptExpression(io.trino.sql.tree.SubscriptExpression) PlanMatchPattern.limit(io.trino.sql.planner.assertions.PlanMatchPattern.limit) PlanMatchPattern.strictProject(io.trino.sql.planner.assertions.PlanMatchPattern.strictProject) ADD(io.trino.sql.tree.ArithmeticBinaryExpression.Operator.ADD) ImmutableList(com.google.common.collect.ImmutableList) BIGINT(io.trino.spi.type.BigintType.BIGINT) PlanMatchPattern.project(io.trino.sql.planner.assertions.PlanMatchPattern.project) SymbolReference(io.trino.sql.tree.SymbolReference) LongLiteral(io.trino.sql.tree.LongLiteral) PlanMatchPattern.sort(io.trino.sql.planner.assertions.PlanMatchPattern.sort) Optional(java.util.Optional) ArithmeticBinaryExpression(io.trino.sql.tree.ArithmeticBinaryExpression) ASCENDING(io.trino.sql.tree.SortItem.Ordering.ASCENDING) LongLiteral(io.trino.sql.tree.LongLiteral) Symbol(io.trino.sql.planner.Symbol) RowType(io.trino.spi.type.RowType) SubscriptExpression(io.trino.sql.tree.SubscriptExpression) BaseRuleTest(io.trino.sql.planner.iterative.rule.test.BaseRuleTest) Test(org.testng.annotations.Test)

Example 87 with RowType

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

the class AvroDecoderTestUtil method checkRowValues.

public static void checkRowValues(Block block, Type type, Object value) {
    assertNotNull(type, "Type is null");
    assertTrue(type instanceof RowType, "Unexpected type");
    assertNotNull(block, "Block is null");
    assertNotNull(value, "Value is null");
    GenericRecord record = (GenericRecord) value;
    RowType rowType = (RowType) type;
    assertEquals(record.getSchema().getFields().size(), rowType.getFields().size(), "Avro field size mismatch");
    assertEquals(block.getPositionCount(), rowType.getFields().size(), "Trino type field size mismatch");
    for (int fieldIndex = 0; fieldIndex < rowType.getFields().size(); fieldIndex++) {
        RowType.Field rowField = rowType.getFields().get(fieldIndex);
        Object expectedValue = record.get(rowField.getName().get());
        if (block.isNull(fieldIndex)) {
            assertNull(expectedValue);
            continue;
        }
        checkField(block, rowField.getType(), fieldIndex, expectedValue);
    }
}
Also used : RowType(io.trino.spi.type.RowType) GenericRecord(org.apache.avro.generic.GenericRecord)

Example 88 with RowType

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

the class AvroDecoderTestUtil method checkMapValues.

public static void checkMapValues(Block block, Type type, Object value) {
    assertNotNull(type, "Type is null");
    assertTrue(type instanceof MapType, "Unexpected type");
    assertTrue(((MapType) type).getKeyType() instanceof VarcharType, "Unexpected key type");
    assertNotNull(block, "Block is null");
    assertNotNull(value, "Value is null");
    Map<String, ?> expected = (Map<String, ?>) value;
    assertEquals(block.getPositionCount(), expected.size() * 2);
    Type valueType = ((MapType) type).getValueType();
    if (valueType instanceof ArrayType) {
        for (int index = 0; index < block.getPositionCount(); index += 2) {
            String actualKey = VARCHAR.getSlice(block, index).toStringUtf8();
            assertTrue(expected.containsKey(actualKey));
            if (block.isNull(index + 1)) {
                assertNull(expected.get(actualKey));
                continue;
            }
            Block arrayBlock = block.getObject(index + 1, Block.class);
            checkArrayValues(arrayBlock, valueType, expected.get(actualKey));
        }
    } else if (valueType instanceof MapType) {
        for (int index = 0; index < block.getPositionCount(); index += 2) {
            String actualKey = VARCHAR.getSlice(block, index).toStringUtf8();
            assertTrue(expected.containsKey(actualKey));
            if (block.isNull(index + 1)) {
                assertNull(expected.get(actualKey));
                continue;
            }
            Block mapBlock = block.getObject(index + 1, Block.class);
            checkMapValues(mapBlock, valueType, expected.get(actualKey));
        }
    } else if (valueType instanceof RowType) {
        for (int index = 0; index < block.getPositionCount(); index += 2) {
            String actualKey = VARCHAR.getSlice(block, index).toStringUtf8();
            assertTrue(expected.containsKey(actualKey));
            if (block.isNull(index + 1)) {
                assertNull(expected.get(actualKey));
                continue;
            }
            Block rowBlock = block.getObject(index + 1, Block.class);
            checkRowValues(rowBlock, valueType, expected.get(actualKey));
        }
    } else {
        for (int index = 0; index < block.getPositionCount(); index += 2) {
            String actualKey = VARCHAR.getSlice(block, index).toStringUtf8();
            assertTrue(expected.containsKey(actualKey));
            checkPrimitiveValue(getObjectValue(valueType, block, index + 1), expected.get(actualKey));
        }
    }
}
Also used : ArrayType(io.trino.spi.type.ArrayType) RowType(io.trino.spi.type.RowType) MapType(io.trino.spi.type.MapType) Type(io.trino.spi.type.Type) ArrayType(io.trino.spi.type.ArrayType) VarcharType(io.trino.spi.type.VarcharType) VarcharType(io.trino.spi.type.VarcharType) Block(io.trino.spi.block.Block) RowType(io.trino.spi.type.RowType) Map(java.util.Map) MapType(io.trino.spi.type.MapType)

Example 89 with RowType

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

the class TestAvroDecoder method testRow.

@Test
public void testRow() {
    Schema schema = SchemaBuilder.record("record_field").fields().name("f1").type().floatType().noDefault().name("f2").type().doubleType().noDefault().name("f3").type().intType().noDefault().name("f4").type().longType().noDefault().name("f5").type().stringType().noDefault().name("f6").type().enumeration("color").symbols("red", "blue", "green").noDefault().name("f7").type().fixed("fixed5").size(5).noDefault().name("f8").type().bytesType().noDefault().name("f9").type().booleanType().noDefault().name("f10").type().array().items().record("sub_array_field").fields().name("sf1").type().stringType().noDefault().name("sf2").type().longType().noDefault().endRecord().noDefault().name("f11").type().map().values().record("sub_map_field").fields().name("sf1").type().doubleType().noDefault().name("sf2").type().booleanType().noDefault().endRecord().noDefault().name("f12").type().record("sub_row_field").fields().name("sf1").type().intType().noDefault().name("sf2").type().enumeration("state").symbols("initialized", "running", "finished", "failed").noDefault().endRecord().noDefault().endRecord();
    RowType rowType = RowType.from(ImmutableList.<RowType.Field>builder().add(RowType.field("f1", REAL)).add(RowType.field("f2", DOUBLE)).add(RowType.field("f3", INTEGER)).add(RowType.field("f4", BIGINT)).add(RowType.field("f5", VARCHAR)).add(RowType.field("f6", VARCHAR)).add(RowType.field("f7", VARBINARY)).add(RowType.field("f8", VARBINARY)).add(RowType.field("f9", BOOLEAN)).add(RowType.field("f10", new ArrayType(RowType.from(ImmutableList.<RowType.Field>builder().add(RowType.field("sf1", VARCHAR)).add(RowType.field("sf2", BIGINT)).build())))).add(RowType.field("f11", MAP_OF_RECORD)).add(RowType.field("f12", RowType.from(ImmutableList.<RowType.Field>builder().add(RowType.field("sf1", INTEGER)).add(RowType.field("sf2", VARCHAR)).build()))).build());
    GenericRecord data = new GenericRecordBuilder(schema).set("f1", 1.5F).set("f2", 1.6D).set("f3", 5).set("f4", 6L).set("f5", "hello").set("f6", new GenericData.EnumSymbol(schema.getField("f6").schema(), "blue")).set("f7", new GenericData.Fixed(schema.getField("f7").schema(), new byte[] { 5, 4, 3, 2, 1 })).set("f8", ByteBuffer.wrap("mytext".getBytes(UTF_8))).set("f9", true).set("f10", ImmutableList.builder().add(new GenericRecordBuilder(schema.getField("f10").schema().getElementType()).set("sf1", "string text").set("sf2", 365_000_000L).build()).add(new GenericRecordBuilder(schema.getField("f10").schema().getElementType()).set("sf1", "more string text").set("sf2", 365_000_000_000L).build()).build()).set("f11", ImmutableMap.builder().put("key1", new GenericRecordBuilder(schema.getField("f11").schema().getValueType()).set("sf1", 3.5D).set("sf2", true).build()).put("key2", new GenericRecordBuilder(schema.getField("f11").schema().getValueType()).set("sf1", 4.5D).set("sf2", false).build()).buildOrThrow()).set("f12", new GenericRecordBuilder(schema.getField("f12").schema()).set("sf1", 3).set("sf2", new GenericData.EnumSymbol(schema.getField("f12").schema().getField("sf2").schema(), "running")).build()).build();
    DecoderTestColumnHandle row = new DecoderTestColumnHandle(0, "record_field", rowType, "record_field", null, null, false, false, false);
    Map<DecoderColumnHandle, FieldValueProvider> decodedRow = buildAndDecodeColumn(row, "record_field", schema.toString(), data);
    checkRowValue(decodedRow, row, data);
}
Also used : DecoderTestColumnHandle(io.trino.decoder.DecoderTestColumnHandle) FieldValueProvider(io.trino.decoder.FieldValueProvider) Schema(org.apache.avro.Schema) RowType(io.trino.spi.type.RowType) DecoderColumnHandle(io.trino.decoder.DecoderColumnHandle) GenericData(org.apache.avro.generic.GenericData) ArrayType(io.trino.spi.type.ArrayType) GenericRecordBuilder(org.apache.avro.generic.GenericRecordBuilder) EnumSymbol(org.apache.avro.generic.GenericData.EnumSymbol) GenericRecord(org.apache.avro.generic.GenericRecord) Test(org.testng.annotations.Test)

Example 90 with RowType

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

the class RaptorStorageManager method toOrcFileType.

static Type toOrcFileType(Type raptorType, TypeManager typeManager) {
    // TIMESTAMPS are stored as BIGINT to void the poor encoding in ORC
    if (raptorType.equals(TIMESTAMP_MILLIS)) {
        return BIGINT;
    }
    if (raptorType instanceof ArrayType) {
        Type elementType = toOrcFileType(((ArrayType) raptorType).getElementType(), typeManager);
        return new ArrayType(elementType);
    }
    if (raptorType instanceof MapType) {
        TypeSignature keyType = toOrcFileType(((MapType) raptorType).getKeyType(), typeManager).getTypeSignature();
        TypeSignature valueType = toOrcFileType(((MapType) raptorType).getValueType(), typeManager).getTypeSignature();
        return typeManager.getParameterizedType(StandardTypes.MAP, ImmutableList.of(TypeSignatureParameter.typeParameter(keyType), TypeSignatureParameter.typeParameter(valueType)));
    }
    if (raptorType instanceof RowType) {
        List<Field> fields = ((RowType) raptorType).getFields().stream().map(field -> new Field(field.getName(), toOrcFileType(field.getType(), typeManager))).collect(toImmutableList());
        return RowType.from(fields);
    }
    return raptorType;
}
Also used : ArrayType(io.trino.spi.type.ArrayType) NamedTypeSignature(io.trino.spi.type.NamedTypeSignature) MoreFutures.allAsList(io.airlift.concurrent.MoreFutures.allAsList) OrcPredicate(io.trino.orc.OrcPredicate) Maps.uniqueIndex(com.google.common.collect.Maps.uniqueIndex) CharType.createCharType(io.trino.spi.type.CharType.createCharType) RAPTOR_RECOVERY_TIMEOUT(io.trino.plugin.raptor.legacy.RaptorErrorCode.RAPTOR_RECOVERY_TIMEOUT) Future(java.util.concurrent.Future) ShardDelta(io.trino.plugin.raptor.legacy.metadata.ShardDelta) Slices(io.airlift.slice.Slices) Map(java.util.Map) RaptorColumnHandle.isShardUuidColumn(io.trino.plugin.raptor.legacy.RaptorColumnHandle.isShardUuidColumn) FileOrcDataSource(io.trino.orc.FileOrcDataSource) RAPTOR_ERROR(io.trino.plugin.raptor.legacy.RaptorErrorCode.RAPTOR_ERROR) ConnectorPageSource(io.trino.spi.connector.ConnectorPageSource) BackupStore(io.trino.plugin.raptor.legacy.backup.BackupStore) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Set(java.util.Set) TypeSignatureParameter(io.trino.spi.type.TypeSignatureParameter) Slice(io.airlift.slice.Slice) OrcFileInfo(io.trino.plugin.raptor.legacy.storage.OrcFileRewriter.OrcFileInfo) TIMESTAMP_MILLIS(io.trino.spi.type.TimestampType.TIMESTAMP_MILLIS) Page(io.trino.spi.Page) BOOLEAN(io.trino.spi.type.BooleanType.BOOLEAN) ShardRecorder(io.trino.plugin.raptor.legacy.metadata.ShardRecorder) ArrayList(java.util.ArrayList) OptionalLong(java.util.OptionalLong) ShardStats.computeColumnStats(io.trino.plugin.raptor.legacy.storage.ShardStats.computeColumnStats) TupleDomainOrcPredicateBuilder(io.trino.orc.TupleDomainOrcPredicate.TupleDomainOrcPredicateBuilder) AggregatedMemoryContext(io.trino.memory.context.AggregatedMemoryContext) VARBINARY(io.trino.spi.type.VarbinaryType.VARBINARY) OrcType(io.trino.orc.metadata.OrcType) ATOMIC_MOVE(java.nio.file.StandardCopyOption.ATOMIC_MOVE) Files(java.nio.file.Files) MapType(io.trino.spi.type.MapType) StandardTypes(io.trino.spi.type.StandardTypes) IOException(java.io.IOException) MoreFutures.getFutureValue(io.airlift.concurrent.MoreFutures.getFutureValue) UTC(org.joda.time.DateTimeZone.UTC) CatalogName(io.trino.plugin.base.CatalogName) Throwables.throwIfInstanceOf(com.google.common.base.Throwables.throwIfInstanceOf) File(java.io.File) RAPTOR_RECOVERY_ERROR(io.trino.plugin.raptor.legacy.RaptorErrorCode.RAPTOR_RECOVERY_ERROR) ExecutionException(java.util.concurrent.ExecutionException) JsonCodec.jsonCodec(io.airlift.json.JsonCodec.jsonCodec) DOUBLE(io.trino.spi.type.DoubleType.DOUBLE) ShardInfo(io.trino.plugin.raptor.legacy.metadata.ShardInfo) OrcColumn(io.trino.orc.OrcColumn) VarcharType.createVarcharType(io.trino.spi.type.VarcharType.createVarcharType) OrcColumnId(io.trino.orc.metadata.OrcColumnId) NodeManager(io.trino.spi.NodeManager) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) TimeoutException(java.util.concurrent.TimeoutException) BackupManager(io.trino.plugin.raptor.legacy.backup.BackupManager) Duration(io.airlift.units.Duration) ColumnInfo(io.trino.plugin.raptor.legacy.metadata.ColumnInfo) PreDestroy(javax.annotation.PreDestroy) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) OrcRecordReader(io.trino.orc.OrcRecordReader) OrcDataSource(io.trino.orc.OrcDataSource) ROOT_COLUMN(io.trino.orc.metadata.OrcColumnId.ROOT_COLUMN) TypeSignature(io.trino.spi.type.TypeSignature) RowType(io.trino.spi.type.RowType) ImmutableSet(com.google.common.collect.ImmutableSet) Field(io.trino.spi.type.RowType.Field) Collection(java.util.Collection) INITIAL_BATCH_SIZE(io.trino.orc.OrcReader.INITIAL_BATCH_SIZE) TrinoException(io.trino.spi.TrinoException) ArrayType(io.trino.spi.type.ArrayType) UUID(java.util.UUID) Math.min(java.lang.Math.min) RaptorColumnHandle(io.trino.plugin.raptor.legacy.RaptorColumnHandle) FileNotFoundException(java.io.FileNotFoundException) Preconditions.checkState(com.google.common.base.Preconditions.checkState) DataSize(io.airlift.units.DataSize) List(java.util.List) BIGINT(io.trino.spi.type.BigintType.BIGINT) Optional(java.util.Optional) DecimalType(io.trino.spi.type.DecimalType) JsonCodec(io.airlift.json.JsonCodec) RaptorColumnHandle.isHiddenColumn(io.trino.plugin.raptor.legacy.RaptorColumnHandle.isHiddenColumn) AggregatedMemoryContext.newSimpleAggregatedMemoryContext(io.trino.memory.context.AggregatedMemoryContext.newSimpleAggregatedMemoryContext) Type(io.trino.spi.type.Type) VarcharType.createUnboundedVarcharType(io.trino.spi.type.VarcharType.createUnboundedVarcharType) CompletableFuture(java.util.concurrent.CompletableFuture) TupleDomainOrcPredicate(io.trino.orc.TupleDomainOrcPredicate) OptionalInt(java.util.OptionalInt) ColumnAdaptation(io.trino.plugin.raptor.legacy.storage.RaptorPageSource.ColumnAdaptation) Inject(javax.inject.Inject) ImmutableList(com.google.common.collect.ImmutableList) Threads.daemonThreadsNamed(io.airlift.concurrent.Threads.daemonThreadsNamed) OrcReaderOptions(io.trino.orc.OrcReaderOptions) Objects.requireNonNull(java.util.Objects.requireNonNull) CompletableFuture.supplyAsync(java.util.concurrent.CompletableFuture.supplyAsync) RowFieldName(io.trino.spi.type.RowFieldName) ExecutorService(java.util.concurrent.ExecutorService) OrcReader(io.trino.orc.OrcReader) RAPTOR_LOCAL_DISK_FULL(io.trino.plugin.raptor.legacy.RaptorErrorCode.RAPTOR_LOCAL_DISK_FULL) PETABYTE(io.airlift.units.DataSize.Unit.PETABYTE) XxHash64(io.airlift.slice.XxHash64) ColumnMetadata(io.trino.orc.metadata.ColumnMetadata) FileInputStream(java.io.FileInputStream) TupleDomain(io.trino.spi.predicate.TupleDomain) Executors.newFixedThreadPool(java.util.concurrent.Executors.newFixedThreadPool) TimeUnit(java.util.concurrent.TimeUnit) RaptorColumnHandle.isShardRowIdColumn(io.trino.plugin.raptor.legacy.RaptorColumnHandle.isShardRowIdColumn) RaptorColumnHandle.isBucketNumberColumn(io.trino.plugin.raptor.legacy.RaptorColumnHandle.isBucketNumberColumn) Collectors.toList(java.util.stream.Collectors.toList) Executors.newCachedThreadPool(java.util.concurrent.Executors.newCachedThreadPool) ColumnStats(io.trino.plugin.raptor.legacy.metadata.ColumnStats) Closeable(java.io.Closeable) VisibleForTesting(com.google.common.annotations.VisibleForTesting) BitSet(java.util.BitSet) TypeManager(io.trino.spi.type.TypeManager) InputStream(java.io.InputStream) Field(io.trino.spi.type.RowType.Field) CharType.createCharType(io.trino.spi.type.CharType.createCharType) OrcType(io.trino.orc.metadata.OrcType) MapType(io.trino.spi.type.MapType) VarcharType.createVarcharType(io.trino.spi.type.VarcharType.createVarcharType) 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.createUnboundedVarcharType(io.trino.spi.type.VarcharType.createUnboundedVarcharType) NamedTypeSignature(io.trino.spi.type.NamedTypeSignature) TypeSignature(io.trino.spi.type.TypeSignature) RowType(io.trino.spi.type.RowType) MapType(io.trino.spi.type.MapType)

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