Search in sources :

Example 1 with SingleRowBlock

use of io.trino.spi.block.SingleRowBlock in project trino by trinodb.

the class TestRowBlock method assertValue.

private void assertValue(Block rowBlock, int position, List<?> row) {
    // null rows are handled by assertPositionValue
    requireNonNull(row, "row is null");
    assertFalse(rowBlock.isNull(position));
    SingleRowBlock singleRowBlock = (SingleRowBlock) rowBlock.getObject(position, Block.class);
    assertEquals(singleRowBlock.getPositionCount(), row.size());
    for (int i = 0; i < row.size(); i++) {
        Object fieldValue = row.get(i);
        if (fieldValue == null) {
            assertTrue(singleRowBlock.isNull(i));
        } else {
            if (fieldValue instanceof Long) {
                assertEquals(BIGINT.getLong(singleRowBlock, i), ((Long) fieldValue).longValue());
            } else if (fieldValue instanceof String) {
                assertEquals(VARCHAR.getSlice(singleRowBlock, i), utf8Slice((String) fieldValue));
            } else {
                throw new IllegalArgumentException();
            }
        }
    }
}
Also used : SingleRowBlock(io.trino.spi.block.SingleRowBlock) Block(io.trino.spi.block.Block) ByteArrayBlock(io.trino.spi.block.ByteArrayBlock) SingleRowBlock(io.trino.spi.block.SingleRowBlock)

Example 2 with SingleRowBlock

use of io.trino.spi.block.SingleRowBlock 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 3 with SingleRowBlock

use of io.trino.spi.block.SingleRowBlock in project trino by trinodb.

the class TestCassandraConnector method testGetTupleType.

@Test
public void testGetTupleType() {
    // TODO add test with nested tuple types
    ConnectorTableHandle tableHandle = getTableHandle(tableTuple);
    ConnectorTableMetadata tableMetadata = metadata.getTableMetadata(SESSION, tableHandle);
    List<ColumnHandle> columnHandles = ImmutableList.copyOf(metadata.getColumnHandles(SESSION, tableHandle).values());
    Map<String, Integer> columnIndex = indexColumns(columnHandles);
    ConnectorTransactionHandle transaction = CassandraTransactionHandle.INSTANCE;
    List<ConnectorSplit> splits = getAllSplits(splitManager.getSplits(transaction, SESSION, tableHandle, UNGROUPED_SCHEDULING, DynamicFilter.EMPTY));
    long rowNumber = 0;
    for (ConnectorSplit split : splits) {
        CassandraSplit cassandraSplit = (CassandraSplit) split;
        long completedBytes = 0;
        try (RecordCursor cursor = recordSetProvider.getRecordSet(transaction, SESSION, cassandraSplit, tableHandle, columnHandles).cursor()) {
            while (cursor.advanceNextPosition()) {
                try {
                    assertReadFields(cursor, tableMetadata.getColumns());
                } catch (RuntimeException e) {
                    throw new RuntimeException("row " + rowNumber, e);
                }
                rowNumber++;
                String keyValue = cursor.getSlice(columnIndex.get("key")).toStringUtf8();
                assertEquals(keyValue, Long.toString(rowNumber));
                SingleRowBlock tupleValueBlock = (SingleRowBlock) cursor.getObject(columnIndex.get("typetuple"));
                assertThat(tupleValueBlock.getPositionCount()).isEqualTo(3);
                CassandraColumnHandle tupleColumnHandle = (CassandraColumnHandle) columnHandles.get(columnIndex.get("typetuple"));
                List<CassandraType> tupleArgumentTypes = tupleColumnHandle.getCassandraType().getArgumentTypes();
                assertThat(tupleArgumentTypes.get(0).getTrinoType().getLong(tupleValueBlock, 0)).isEqualTo(rowNumber);
                assertThat(tupleArgumentTypes.get(1).getTrinoType().getSlice(tupleValueBlock, 1).toStringUtf8()).isEqualTo("text-" + rowNumber);
                assertThat(tupleArgumentTypes.get(2).getTrinoType().getLong(tupleValueBlock, 2)).isEqualTo(Float.floatToRawIntBits(1.11f * rowNumber));
                long newCompletedBytes = cursor.getCompletedBytes();
                assertTrue(newCompletedBytes >= completedBytes);
                completedBytes = newCompletedBytes;
            }
        }
    }
    assertEquals(rowNumber, 2);
}
Also used : ColumnHandle(io.trino.spi.connector.ColumnHandle) RecordCursor(io.trino.spi.connector.RecordCursor) ConnectorTransactionHandle(io.trino.spi.connector.ConnectorTransactionHandle) ConnectorTableHandle(io.trino.spi.connector.ConnectorTableHandle) SingleRowBlock(io.trino.spi.block.SingleRowBlock) ConnectorSplit(io.trino.spi.connector.ConnectorSplit) ConnectorTableMetadata(io.trino.spi.connector.ConnectorTableMetadata) Test(org.testng.annotations.Test)

Example 4 with SingleRowBlock

use of io.trino.spi.block.SingleRowBlock in project trino by trinodb.

the class TestCassandraConnector method testGetUserDefinedType.

@Test
public void testGetUserDefinedType() {
    ConnectorTableHandle tableHandle = getTableHandle(tableUdt);
    ConnectorTableMetadata tableMetadata = metadata.getTableMetadata(SESSION, tableHandle);
    List<ColumnHandle> columnHandles = ImmutableList.copyOf(metadata.getColumnHandles(SESSION, tableHandle).values());
    Map<String, Integer> columnIndex = indexColumns(columnHandles);
    ConnectorTransactionHandle transaction = CassandraTransactionHandle.INSTANCE;
    tableHandle = metadata.applyFilter(SESSION, tableHandle, Constraint.alwaysTrue()).get().getHandle();
    List<ConnectorSplit> splits = getAllSplits(splitManager.getSplits(transaction, SESSION, tableHandle, UNGROUPED_SCHEDULING, DynamicFilter.EMPTY));
    long rowNumber = 0;
    for (ConnectorSplit split : splits) {
        CassandraSplit cassandraSplit = (CassandraSplit) split;
        long completedBytes = 0;
        try (RecordCursor cursor = recordSetProvider.getRecordSet(transaction, SESSION, cassandraSplit, tableHandle, columnHandles).cursor()) {
            while (cursor.advanceNextPosition()) {
                try {
                    assertReadFields(cursor, tableMetadata.getColumns());
                } catch (RuntimeException e) {
                    throw new RuntimeException("row " + rowNumber, e);
                }
                rowNumber++;
                String keyValue = cursor.getSlice(columnIndex.get("key")).toStringUtf8();
                SingleRowBlock udtValue = (SingleRowBlock) cursor.getObject(columnIndex.get("typeudt"));
                assertEquals(keyValue, "key");
                assertEquals(VARCHAR.getSlice(udtValue, 0).toStringUtf8(), "text");
                assertEquals(trinoUuidToJavaUuid(UUID.getSlice(udtValue, 1)).toString(), "01234567-0123-0123-0123-0123456789ab");
                assertEquals(INTEGER.getLong(udtValue, 2), -2147483648);
                assertEquals(BIGINT.getLong(udtValue, 3), -9223372036854775808L);
                assertEquals(VARBINARY.getSlice(udtValue, 4).toStringUtf8(), "01234");
                assertEquals(TIMESTAMP.getLong(udtValue, 5), 117964800000L);
                assertEquals(VARCHAR.getSlice(udtValue, 6).toStringUtf8(), "ansi");
                assertTrue(BOOLEAN.getBoolean(udtValue, 7));
                assertEquals(DOUBLE.getDouble(udtValue, 8), 99999999999999997748809823456034029568D);
                assertEquals(DOUBLE.getDouble(udtValue, 9), 4.9407e-324);
                assertEquals(REAL.getObjectValue(SESSION, udtValue, 10), 1.4E-45f);
                assertEquals(VARCHAR.getSlice(udtValue, 11).toStringUtf8(), "0.0.0.0");
                assertEquals(VARCHAR.getSlice(udtValue, 12).toStringUtf8(), "varchar");
                assertEquals(VARCHAR.getSlice(udtValue, 13).toStringUtf8(), "-9223372036854775808");
                assertEquals(trinoUuidToJavaUuid(UUID.getSlice(udtValue, 14)).toString(), "d2177dd0-eaa2-11de-a572-001b779c76e3");
                assertEquals(VARCHAR.getSlice(udtValue, 15).toStringUtf8(), "[\"list\"]");
                assertEquals(VARCHAR.getSlice(udtValue, 16).toStringUtf8(), "{\"map\":1}");
                assertEquals(VARCHAR.getSlice(udtValue, 17).toStringUtf8(), "[true]");
                SingleRowBlock tupleValueBlock = (SingleRowBlock) udtValue.getObject(18, Block.class);
                assertThat(tupleValueBlock.getPositionCount()).isEqualTo(1);
                assertThat(INTEGER.getLong(tupleValueBlock, 0)).isEqualTo(123);
                SingleRowBlock udtValueBlock = (SingleRowBlock) udtValue.getObject(19, Block.class);
                assertThat(udtValueBlock.getPositionCount()).isEqualTo(1);
                assertThat(INTEGER.getLong(udtValueBlock, 0)).isEqualTo(999);
                long newCompletedBytes = cursor.getCompletedBytes();
                assertTrue(newCompletedBytes >= completedBytes);
                completedBytes = newCompletedBytes;
            }
        }
    }
    assertEquals(rowNumber, 1);
}
Also used : ColumnHandle(io.trino.spi.connector.ColumnHandle) RecordCursor(io.trino.spi.connector.RecordCursor) ConnectorTransactionHandle(io.trino.spi.connector.ConnectorTransactionHandle) ConnectorTableHandle(io.trino.spi.connector.ConnectorTableHandle) SingleRowBlock(io.trino.spi.block.SingleRowBlock) Block(io.trino.spi.block.Block) SingleRowBlock(io.trino.spi.block.SingleRowBlock) ConnectorSplit(io.trino.spi.connector.ConnectorSplit) ConnectorTableMetadata(io.trino.spi.connector.ConnectorTableMetadata) Test(org.testng.annotations.Test)

Aggregations

SingleRowBlock (io.trino.spi.block.SingleRowBlock)4 Block (io.trino.spi.block.Block)2 ColumnHandle (io.trino.spi.connector.ColumnHandle)2 ConnectorSplit (io.trino.spi.connector.ConnectorSplit)2 ConnectorTableHandle (io.trino.spi.connector.ConnectorTableHandle)2 ConnectorTableMetadata (io.trino.spi.connector.ConnectorTableMetadata)2 ConnectorTransactionHandle (io.trino.spi.connector.ConnectorTransactionHandle)2 RecordCursor (io.trino.spi.connector.RecordCursor)2 Test (org.testng.annotations.Test)2 Preconditions.checkState (com.google.common.base.Preconditions.checkState)1 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 Session (io.trino.Session)1 Rule (io.trino.cost.ComposableStatsCalculator.Rule)1 Pattern (io.trino.matching.Pattern)1 AllowAllAccessControl (io.trino.security.AllowAllAccessControl)1 ByteArrayBlock (io.trino.spi.block.ByteArrayBlock)1 StatsUtil.toStatsRepresentation (io.trino.spi.statistics.StatsUtil.toStatsRepresentation)1 RowType (io.trino.spi.type.RowType)1 Type (io.trino.spi.type.Type)1