Search in sources :

Example 21 with RecordCursor

use of io.trino.spi.connector.RecordCursor in project trino by trinodb.

the class TestJdbcRecordSet method testCursorSimple.

@Test
public void testCursorSimple() {
    RecordSet recordSet = createRecordSet(ImmutableList.of(columnHandles.get("text"), columnHandles.get("text_short"), columnHandles.get("value")));
    try (RecordCursor cursor = recordSet.cursor()) {
        assertEquals(cursor.getType(0), VARCHAR);
        assertEquals(cursor.getType(1), createVarcharType(32));
        assertEquals(cursor.getType(2), BIGINT);
        Map<String, Long> data = new LinkedHashMap<>();
        while (cursor.advanceNextPosition()) {
            data.put(cursor.getSlice(0).toStringUtf8(), cursor.getLong(2));
            assertEquals(cursor.getSlice(0), cursor.getSlice(1));
            assertFalse(cursor.isNull(0));
            assertFalse(cursor.isNull(1));
            assertFalse(cursor.isNull(2));
        }
        assertEquals(data, ImmutableMap.<String, Long>builder().put("one", 1L).put("two", 2L).put("three", 3L).put("ten", 10L).put("eleven", 11L).put("twelve", 12L).buildOrThrow());
    }
}
Also used : RecordCursor(io.trino.spi.connector.RecordCursor) RecordSet(io.trino.spi.connector.RecordSet) LinkedHashMap(java.util.LinkedHashMap) Test(org.testng.annotations.Test)

Example 22 with RecordCursor

use of io.trino.spi.connector.RecordCursor in project trino by trinodb.

the class TestJdbcRecordSet method testCursorMixedOrder.

@Test
public void testCursorMixedOrder() {
    RecordSet recordSet = createRecordSet(ImmutableList.of(columnHandles.get("value"), columnHandles.get("value"), columnHandles.get("text")));
    try (RecordCursor cursor = recordSet.cursor()) {
        assertEquals(cursor.getType(0), BIGINT);
        assertEquals(cursor.getType(1), BIGINT);
        assertEquals(cursor.getType(2), VARCHAR);
        Map<String, Long> data = new LinkedHashMap<>();
        while (cursor.advanceNextPosition()) {
            assertEquals(cursor.getLong(0), cursor.getLong(1));
            data.put(cursor.getSlice(2).toStringUtf8(), cursor.getLong(0));
        }
        assertEquals(data, ImmutableMap.<String, Long>builder().put("one", 1L).put("two", 2L).put("three", 3L).put("ten", 10L).put("eleven", 11L).put("twelve", 12L).buildOrThrow());
    }
}
Also used : RecordCursor(io.trino.spi.connector.RecordCursor) RecordSet(io.trino.spi.connector.RecordSet) LinkedHashMap(java.util.LinkedHashMap) Test(org.testng.annotations.Test)

Example 23 with RecordCursor

use of io.trino.spi.connector.RecordCursor in project trino by trinodb.

the class GenericHiveRecordCursorProvider method createRecordCursor.

@Override
public Optional<ReaderRecordCursorWithProjections> createRecordCursor(Configuration configuration, ConnectorSession session, Path path, long start, long length, long fileSize, Properties schema, List<HiveColumnHandle> columns, TupleDomain<HiveColumnHandle> effectivePredicate, TypeManager typeManager, boolean s3SelectPushdownEnabled) {
    configuration.setInt(LineRecordReader.MAX_LINE_LENGTH, textMaxLineLengthBytes);
    // make sure the FileSystem is created with the proper Configuration object
    try {
        this.hdfsEnvironment.getFileSystem(session.getIdentity(), path, configuration);
    } catch (IOException e) {
        throw new TrinoException(HIVE_FILESYSTEM_ERROR, "Failed getting FileSystem: " + path, e);
    }
    Optional<ReaderColumns> projections = projectBaseColumns(columns);
    List<HiveColumnHandle> readerColumns = projections.map(ReaderColumns::get).map(columnHandles -> columnHandles.stream().map(HiveColumnHandle.class::cast).collect(toUnmodifiableList())).orElse(columns);
    RecordCursor cursor = hdfsEnvironment.doAs(session.getIdentity(), () -> {
        RecordReader<?, ?> recordReader = HiveUtil.createRecordReader(configuration, path, start, length, schema, readerColumns);
        try {
            return new GenericHiveRecordCursor<>(configuration, path, genericRecordReader(recordReader), length, schema, readerColumns);
        } catch (Exception e) {
            try {
                recordReader.close();
            } catch (IOException closeException) {
                if (e != closeException) {
                    e.addSuppressed(closeException);
                }
            }
            throw e;
        }
    });
    return Optional.of(new ReaderRecordCursorWithProjections(cursor, projections));
}
Also used : HiveUtil(io.trino.plugin.hive.util.HiveUtil) RecordCursor(io.trino.spi.connector.RecordCursor) Properties(java.util.Properties) LineRecordReader(org.apache.hadoop.mapreduce.lib.input.LineRecordReader) TrinoException(io.trino.spi.TrinoException) Writable(org.apache.hadoop.io.Writable) IOException(java.io.IOException) ConnectorSession(io.trino.spi.connector.ConnectorSession) TupleDomain(io.trino.spi.predicate.TupleDomain) Collectors.toUnmodifiableList(java.util.stream.Collectors.toUnmodifiableList) Inject(javax.inject.Inject) DataSize(io.airlift.units.DataSize) List(java.util.List) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) HivePageSourceProvider.projectBaseColumns(io.trino.plugin.hive.HivePageSourceProvider.projectBaseColumns) Configuration(org.apache.hadoop.conf.Configuration) Objects.requireNonNull(java.util.Objects.requireNonNull) Path(org.apache.hadoop.fs.Path) RecordReader(org.apache.hadoop.mapred.RecordReader) Optional(java.util.Optional) HIVE_FILESYSTEM_ERROR(io.trino.plugin.hive.HiveErrorCode.HIVE_FILESYSTEM_ERROR) Math.toIntExact(java.lang.Math.toIntExact) TypeManager(io.trino.spi.type.TypeManager) RecordCursor(io.trino.spi.connector.RecordCursor) TrinoException(io.trino.spi.TrinoException) IOException(java.io.IOException) TrinoException(io.trino.spi.TrinoException) IOException(java.io.IOException)

Example 24 with RecordCursor

use of io.trino.spi.connector.RecordCursor in project trino by trinodb.

the class TestExampleRecordSetProvider method testGetRecordSet.

@Test
public void testGetRecordSet() {
    ConnectorTableHandle tableHandle = new ExampleTableHandle("schema", "table");
    ExampleRecordSetProvider recordSetProvider = new ExampleRecordSetProvider();
    RecordSet recordSet = recordSetProvider.getRecordSet(ExampleTransactionHandle.INSTANCE, SESSION, new ExampleSplit(dataUri), tableHandle, ImmutableList.of(new ExampleColumnHandle("text", createUnboundedVarcharType(), 0), new ExampleColumnHandle("value", BIGINT, 1)));
    assertNotNull(recordSet, "recordSet is null");
    RecordCursor cursor = recordSet.cursor();
    assertNotNull(cursor, "cursor is null");
    Map<String, Long> data = new LinkedHashMap<>();
    while (cursor.advanceNextPosition()) {
        data.put(cursor.getSlice(0).toStringUtf8(), cursor.getLong(1));
    }
    assertEquals(data, ImmutableMap.<String, Long>builder().put("ten", 10L).put("eleven", 11L).put("twelve", 12L).buildOrThrow());
}
Also used : RecordCursor(io.trino.spi.connector.RecordCursor) RecordSet(io.trino.spi.connector.RecordSet) ConnectorTableHandle(io.trino.spi.connector.ConnectorTableHandle) LinkedHashMap(java.util.LinkedHashMap) Test(org.testng.annotations.Test)

Example 25 with RecordCursor

use of io.trino.spi.connector.RecordCursor in project trino by trinodb.

the class TestExampleRecordSet method testCursorMixedOrder.

@Test
public void testCursorMixedOrder() {
    RecordSet recordSet = new ExampleRecordSet(new ExampleSplit(dataUri), ImmutableList.of(new ExampleColumnHandle("value", BIGINT, 1), new ExampleColumnHandle("value", BIGINT, 1), new ExampleColumnHandle("text", createUnboundedVarcharType(), 0)));
    RecordCursor cursor = recordSet.cursor();
    Map<String, Long> data = new LinkedHashMap<>();
    while (cursor.advanceNextPosition()) {
        assertEquals(cursor.getLong(0), cursor.getLong(1));
        data.put(cursor.getSlice(2).toStringUtf8(), cursor.getLong(0));
    }
    assertEquals(data, ImmutableMap.<String, Long>builder().put("ten", 10L).put("eleven", 11L).put("twelve", 12L).buildOrThrow());
}
Also used : RecordCursor(io.trino.spi.connector.RecordCursor) RecordSet(io.trino.spi.connector.RecordSet) LinkedHashMap(java.util.LinkedHashMap) Test(org.testng.annotations.Test)

Aggregations

RecordCursor (io.trino.spi.connector.RecordCursor)32 Test (org.testng.annotations.Test)18 RecordSet (io.trino.spi.connector.RecordSet)12 LinkedHashMap (java.util.LinkedHashMap)8 List (java.util.List)8 Optional (java.util.Optional)6 ImmutableList (com.google.common.collect.ImmutableList)5 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)5 Block (io.trino.spi.block.Block)5 TupleDomain (io.trino.spi.predicate.TupleDomain)5 Type (io.trino.spi.type.Type)5 ArrayList (java.util.ArrayList)5 Map (java.util.Map)5 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)4 ColumnHandle (io.trino.spi.connector.ColumnHandle)4 ConnectorTableHandle (io.trino.spi.connector.ConnectorTableHandle)4 ConnectorTableMetadata (io.trino.spi.connector.ConnectorTableMetadata)4 ConnectorTransactionHandle (io.trino.spi.connector.ConnectorTransactionHandle)4 Objects.requireNonNull (java.util.Objects.requireNonNull)4 Slice (io.airlift.slice.Slice)3