Java Code Examples for io.prestosql.spi.connector.RecordCursor

The following examples show how to use io.prestosql.spi.connector.RecordCursor. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: presto   Source File: TestExampleRecordSetProvider.java    License: Apache License 2.0 6 votes vote down vote up
@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)
            .build());
}
 
Example 2
Source Project: presto   Source File: TestExampleRecordSet.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testCursorSimple()
{
    RecordSet recordSet = new ExampleRecordSet(new ExampleSplit(dataUri), ImmutableList.of(
            new ExampleColumnHandle("text", createUnboundedVarcharType(), 0),
            new ExampleColumnHandle("value", BIGINT, 1)));
    RecordCursor cursor = recordSet.cursor();

    assertEquals(cursor.getType(0), createUnboundedVarcharType());
    assertEquals(cursor.getType(1), BIGINT);

    Map<String, Long> data = new LinkedHashMap<>();
    while (cursor.advanceNextPosition()) {
        data.put(cursor.getSlice(0).toStringUtf8(), cursor.getLong(1));
        assertFalse(cursor.isNull(0));
        assertFalse(cursor.isNull(1));
    }
    assertEquals(data, ImmutableMap.<String, Long>builder()
            .put("ten", 10L)
            .put("eleven", 11L)
            .put("twelve", 12L)
            .build());
}
 
Example 3
Source Project: presto   Source File: TestExampleRecordSet.java    License: Apache License 2.0 6 votes vote down vote up
@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)
            .build());
}
 
Example 4
Source Project: presto   Source File: TestJmxSplitManager.java    License: Apache License 2.0 6 votes vote down vote up
private List<Long> readTimeStampsFrom(RecordSet recordSet)
{
    ImmutableList.Builder<Long> result = ImmutableList.builder();
    try (RecordCursor cursor = recordSet.cursor()) {
        while (cursor.advanceNextPosition()) {
            for (int i = 0; i < recordSet.getColumnTypes().size(); i++) {
                cursor.isNull(i);
            }
            if (cursor.isNull(0)) {
                return result.build();
            }
            assertTrue(recordSet.getColumnTypes().get(0) instanceof TimestampType);
            result.add(cursor.getLong(0));
        }
    }
    return result.build();
}
 
Example 5
Source Project: presto   Source File: TestJdbcRecordSetProvider.java    License: Apache License 2.0 6 votes vote down vote up
private RecordCursor getCursor(JdbcTableHandle jdbcTableHandle, List<JdbcColumnHandle> columns, TupleDomain<ColumnHandle> domain)
{
    jdbcTableHandle = new JdbcTableHandle(
            jdbcTableHandle.getSchemaTableName(),
            jdbcTableHandle.getCatalogName(),
            jdbcTableHandle.getSchemaName(),
            jdbcTableHandle.getTableName(),
            domain,
            Optional.empty(),
            OptionalLong.empty(),
            Optional.empty());

    ConnectorSplitSource splits = jdbcClient.getSplits(SESSION, jdbcTableHandle);
    JdbcSplit split = (JdbcSplit) getOnlyElement(getFutureValue(splits.getNextBatch(NOT_PARTITIONED, 1000)).getSplits());

    ConnectorTransactionHandle transaction = new JdbcTransactionHandle();
    JdbcRecordSetProvider recordSetProvider = new JdbcRecordSetProvider(jdbcClient);
    RecordSet recordSet = recordSetProvider.getRecordSet(transaction, SESSION, split, jdbcTableHandle, columns);

    return recordSet.cursor();
}
 
Example 6
Source Project: presto   Source File: PrestoThriftTypeUtils.java    License: Apache License 2.0 6 votes vote down vote up
public static PrestoThriftBlock fromLongBasedColumn(RecordSet recordSet, int columnIndex, int positions, BiFunction<boolean[], long[], PrestoThriftBlock> result)
{
    if (positions == 0) {
        return result.apply(null, null);
    }
    boolean[] nulls = null;
    long[] longs = null;
    RecordCursor cursor = recordSet.cursor();
    for (int position = 0; position < positions; position++) {
        checkState(cursor.advanceNextPosition(), "cursor has less values than expected");
        if (cursor.isNull(columnIndex)) {
            if (nulls == null) {
                nulls = new boolean[positions];
            }
            nulls[position] = true;
        }
        else {
            if (longs == null) {
                longs = new long[positions];
            }
            longs[position] = cursor.getLong(columnIndex);
        }
    }
    checkState(!cursor.advanceNextPosition(), "cursor has more values than expected");
    return result.apply(nulls, longs);
}
 
Example 7
Source Project: presto   Source File: PrestoThriftTypeUtils.java    License: Apache License 2.0 6 votes vote down vote up
public static PrestoThriftBlock fromIntBasedColumn(RecordSet recordSet, int columnIndex, int positions, BiFunction<boolean[], int[], PrestoThriftBlock> result)
{
    if (positions == 0) {
        return result.apply(null, null);
    }
    boolean[] nulls = null;
    int[] ints = null;
    RecordCursor cursor = recordSet.cursor();
    for (int position = 0; position < positions; position++) {
        checkState(cursor.advanceNextPosition(), "cursor has less values than expected");
        if (cursor.isNull(columnIndex)) {
            if (nulls == null) {
                nulls = new boolean[positions];
            }
            nulls[position] = true;
        }
        else {
            if (ints == null) {
                ints = new int[positions];
            }
            ints[position] = (int) cursor.getLong(columnIndex);
        }
    }
    checkState(!cursor.advanceNextPosition(), "cursor has more values than expected");
    return result.apply(nulls, ints);
}
 
Example 8
Source Project: presto   Source File: TableStatisticsRecorder.java    License: Apache License 2.0 6 votes vote down vote up
private Comparable<?> getPrestoValue(RecordCursor recordCursor, List<Column> columns, int columnId)
{
    if (recordCursor.isNull(columnId)) {
        return null;
    }

    Column column = columns.get(columnId);
    ColumnType.Base baseType = column.getType().getBase();
    switch (baseType) {
        case IDENTIFIER:
        case INTEGER:
        case DATE:
        case TIME:
        case DECIMAL:
            return recordCursor.getLong(columnId);
        case VARCHAR:
        case CHAR:
            return recordCursor.getSlice(columnId).toStringAscii();
    }
    throw new UnsupportedOperationException(format("Unsupported TPCDS base type [%s]", baseType));
}
 
Example 9
Source Project: presto   Source File: TpchIndexedData.java    License: Apache License 2.0 6 votes vote down vote up
private static IndexedTable indexTable(RecordSet recordSet, final List<String> outputColumns, List<String> keyColumns)
{
    List<Integer> keyPositions = keyColumns.stream()
            .map(columnName -> {
                int position = outputColumns.indexOf(columnName);
                checkState(position != -1);
                return position;
            })
            .collect(toImmutableList());

    ImmutableListMultimap.Builder<MaterializedTuple, MaterializedTuple> indexedValuesBuilder = ImmutableListMultimap.builder();

    List<Type> outputTypes = recordSet.getColumnTypes();
    List<Type> keyTypes = extractPositionValues(outputTypes, keyPositions);

    RecordCursor cursor = recordSet.cursor();
    while (cursor.advanceNextPosition()) {
        List<Object> values = extractValues(cursor, outputTypes);
        List<Object> keyValues = extractPositionValues(values, keyPositions);

        indexedValuesBuilder.put(new MaterializedTuple(keyValues), new MaterializedTuple(values));
    }

    return new IndexedTable(keyColumns, keyTypes, outputColumns, outputTypes, indexedValuesBuilder.build());
}
 
Example 10
Source Project: presto   Source File: TpchIndexedData.java    License: Apache License 2.0 6 votes vote down vote up
private static Object extractObject(RecordCursor cursor, int field, Type type)
{
    if (cursor.isNull(field)) {
        return null;
    }

    Class<?> javaType = type.getJavaType();
    if (javaType == boolean.class) {
        return cursor.getBoolean(field);
    }
    if (javaType == long.class) {
        return cursor.getLong(field);
    }
    if (javaType == double.class) {
        return cursor.getDouble(field);
    }
    if (javaType == Slice.class) {
        return cursor.getSlice(field).toStringUtf8();
    }
    throw new AssertionError("Unsupported type: " + type);
}
 
Example 11
public HiveReaderProjectionsAdaptingRecordCursor(RecordCursor delegate, ReaderProjectionsAdapter projectionsAdapter)
{
    this.delegate = requireNonNull(delegate, "delegate is null");
    requireNonNull(projectionsAdapter, "projectionsAdapter is null");

    this.channelMappings = new ChannelMapping[projectionsAdapter.getOutputToInputMapping().size()];
    projectionsAdapter.getOutputToInputMapping().toArray(channelMappings);

    this.outputTypes = new Type[projectionsAdapter.getOutputTypes().size()];
    projectionsAdapter.getOutputTypes().toArray(outputTypes);

    this.inputTypes = new Type[projectionsAdapter.getInputTypes().size()];
    projectionsAdapter.getInputTypes().toArray(inputTypes);

    this.baseTypes = new Type[outputTypes.length];
    for (int i = 0; i < baseTypes.length; i++) {
        Type type = inputTypes[channelMappings[i].getInputChannelIndex()];
        List<Integer> dereferences = channelMappings[i].getDereferenceSequence();
        for (int j = 0; j < dereferences.size(); j++) {
            type = type.getTypeParameters().get(dereferences.get(j));
        }
        baseTypes[i] = type;
    }
}
 
Example 12
Source Project: presto   Source File: TestListBasedRecordSet.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testCursor()
{
    ListBasedRecordSet recordSet = new ListBasedRecordSet(
            ImmutableList.of(
                    Arrays.asList("1", null, "3"),
                    Arrays.asList("ab", "c", null)),
            ImmutableList.of(BIGINT, VARCHAR));
    assertEquals(recordSet.getColumnTypes(), ImmutableList.of(BIGINT, VARCHAR));
    RecordCursor cursor = recordSet.cursor();
    assertTrue(cursor.advanceNextPosition());
    assertEquals(cursor.getType(0), BIGINT);
    assertEquals(cursor.getType(1), VARCHAR);
    assertThrows(IndexOutOfBoundsException.class, () -> cursor.getLong(2));
    assertEquals(cursor.getLong(0), 1L);
    assertEquals(cursor.getSlice(1), Slices.utf8Slice("ab"));
    assertTrue(cursor.advanceNextPosition());
    assertTrue(cursor.isNull(0));
    assertEquals(cursor.getSlice(1), Slices.utf8Slice("c"));
    assertTrue(cursor.advanceNextPosition());
    assertEquals(cursor.getLong(0), 3L);
    assertTrue(cursor.isNull(1));
    assertFalse(cursor.advanceNextPosition());
    assertThrows(IndexOutOfBoundsException.class, () -> cursor.getLong(0));
}
 
Example 13
Source Project: presto   Source File: SystemPageSourceProvider.java    License: Apache License 2.0 6 votes vote down vote up
private static RecordSet toRecordSet(ConnectorTransactionHandle sourceTransaction, SystemTable table, ConnectorSession session, TupleDomain<Integer> constraint)
{
    return new RecordSet()
    {
        private final List<Type> types = table.getTableMetadata().getColumns().stream()
                .map(ColumnMetadata::getType)
                .collect(toImmutableList());

        @Override
        public List<Type> getColumnTypes()
        {
            return types;
        }

        @Override
        public RecordCursor cursor()
        {
            return table.cursor(sourceTransaction, session, constraint);
        }
    };
}
 
Example 14
Source Project: presto   Source File: TransactionsSystemTable.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public RecordCursor cursor(ConnectorTransactionHandle transactionHandle, ConnectorSession session, TupleDomain<Integer> constraint)
{
    Builder table = InMemoryRecordSet.builder(transactionsTable);
    for (TransactionInfo info : transactionManager.getAllTransactionInfos()) {
        table.addRow(
                info.getTransactionId().toString(),
                info.getIsolationLevel().toString(),
                info.isReadOnly(),
                info.isAutoCommitContext(),
                info.getCreateTime().getMillis(),
                (long) info.getIdleTime().getValue(TimeUnit.SECONDS),
                info.getWrittenConnectorId().map(CatalogName::getCatalogName).orElse(null),
                createStringsBlock(info.getCatalogNames()));
    }
    return table.build().cursor();
}
 
Example 15
Source Project: presto   Source File: TestFieldSetFilteringRecordSet.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void test()
{
    ArrayType arrayOfBigintType = new ArrayType(BIGINT);
    FieldSetFilteringRecordSet fieldSetFilteringRecordSet = new FieldSetFilteringRecordSet(
            createTestMetadataManager(),
            new InMemoryRecordSet(
                    ImmutableList.of(BIGINT, BIGINT, TIMESTAMP_WITH_TIME_ZONE, TIMESTAMP_WITH_TIME_ZONE, arrayOfBigintType, arrayOfBigintType),
                    ImmutableList.of(
                            ImmutableList.of(
                                    100L,
                                    100L,
                                    // test same time in different time zone to make sure equal check was done properly
                                    packDateTimeWithZone(100, getTimeZoneKeyForOffset(123)),
                                    packDateTimeWithZone(100, getTimeZoneKeyForOffset(234)),
                                    // test structural type
                                    arrayBlockOf(BIGINT, 12, 34, 56),
                                    arrayBlockOf(BIGINT, 12, 34, 56)))),
            ImmutableList.of(ImmutableSet.of(0, 1), ImmutableSet.of(2, 3), ImmutableSet.of(4, 5)));
    RecordCursor recordCursor = fieldSetFilteringRecordSet.cursor();
    assertTrue(recordCursor.advanceNextPosition());
}
 
Example 16
Source Project: presto   Source File: KuduRecordSet.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public RecordCursor cursor()
{
    KuduScanner scanner = clientSession.createScanner(kuduSplit);
    Schema projectedSchema = scanner.getProjectionSchema();
    ImmutableMap.Builder<Integer, Integer> builder = ImmutableMap.builder();
    for (int i = 0; i < columns.size(); i++) {
        KuduColumnHandle handle = (KuduColumnHandle) columns.get(i);
        if (handle.isVirtualRowId()) {
            builder.put(i, ROW_ID_POSITION);
        }
        else {
            builder.put(i, projectedSchema.getColumnIndex(handle.getName()));
        }
    }

    return new KuduRecordCursor(scanner, getTable(), getColumnTypes(), builder.build());
}
 
Example 17
Source Project: presto   Source File: TestPrometheusRecordSetProvider.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testGetRecordSet()
{
    ConnectorTableHandle tableHandle = new PrometheusTableHandle("schema", "table");
    PrometheusRecordSetProvider recordSetProvider = new PrometheusRecordSetProvider();
    RecordSet recordSet = recordSetProvider.getRecordSet(PrometheusTransactionHandle.INSTANCE, SESSION,
            new PrometheusSplit(dataUri), tableHandle, ImmutableList.of(
                    new PrometheusColumnHandle("labels", varcharMapType, 0),
                    new PrometheusColumnHandle("timestamp", TimestampType.TIMESTAMP, 1),
                    new PrometheusColumnHandle("value", DoubleType.DOUBLE, 2)));
    assertNotNull(recordSet, "recordSet is null");

    RecordCursor cursor = recordSet.cursor();
    assertNotNull(cursor, "cursor is null");

    Map<Timestamp, Map> actual = new LinkedHashMap<>();
    while (cursor.advanceNextPosition()) {
        actual.put((Timestamp) cursor.getObject(1), (Map) getMapFromBlock(varcharMapType, (Block) cursor.getObject(0)));
    }
    Map<Timestamp, Map> expected = ImmutableMap.<Timestamp, Map>builder()
            .put(Timestamp.from(Instant.ofEpochMilli(1565962969044L)), ImmutableMap.of("instance",
                    "localhost:9090", "__name__", "up", "job", "prometheus"))
            .put(Timestamp.from(Instant.ofEpochMilli(1565962984045L)), ImmutableMap.of("instance",
                    "localhost:9090", "__name__", "up", "job", "prometheus"))
            .put(Timestamp.from(Instant.ofEpochMilli(1565962999044L)), ImmutableMap.of("instance",
                    "localhost:9090", "__name__", "up", "job", "prometheus"))
            .put(Timestamp.from(Instant.ofEpochMilli(1565963014044L)), ImmutableMap.of("instance",
                    "localhost:9090", "__name__", "up", "job", "prometheus"))
            .build();
    assertEquals(actual, expected);
}
 
Example 18
Source Project: presto   Source File: TestJmxSplitManager.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testRecordSetProvider()
        throws Exception
{
    for (SchemaTableName schemaTableName : metadata.listTables(SESSION, Optional.of(JMX_SCHEMA_NAME))) {
        RecordSet recordSet = getRecordSet(schemaTableName);
        try (RecordCursor cursor = recordSet.cursor()) {
            while (cursor.advanceNextPosition()) {
                for (int i = 0; i < recordSet.getColumnTypes().size(); i++) {
                    cursor.isNull(i);
                }
            }
        }
    }
}
 
Example 19
Source Project: presto   Source File: HiveCoercionRecordCursor.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void coerce(RecordCursor delegate, int field)
{
    if (delegate.isNull(field)) {
        setIsNull(true);
        return;
    }
    Block block = (Block) delegate.getObject(field);
    if (pageBuilder.isFull()) {
        pageBuilder.reset();
    }
    BlockBuilder blockBuilder = pageBuilder.getBlockBuilder(0);
    BlockBuilder mapBuilder = blockBuilder.beginBlockEntry();
    for (int i = 0; i < block.getPositionCount(); i++) {
        int k = i % 2;
        if (coercers[k] == null) {
            toKeyValueTypes.get(k).appendTo(block, i, mapBuilder);
        }
        else {
            if (block.isNull(i)) {
                mapBuilder.appendNull();
            }
            else {
                rewriteBlock(fromKeyValueTypes.get(k), toKeyValueTypes.get(k), block, i, mapBuilder, coercers[k], bridgingRecordCursor);
            }
        }
    }
    blockBuilder.closeEntry();
    pageBuilder.declarePosition();
    setObject(toType.getObject(blockBuilder, blockBuilder.getPositionCount() - 1));
}
 
Example 20
Source Project: presto   Source File: TestJdbcRecordSet.java    License: Apache License 2.0 5 votes vote down vote up
@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)
                .build());
    }
}
 
Example 21
Source Project: presto   Source File: TestJdbcRecordSet.java    License: Apache License 2.0 5 votes vote down vote up
@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)
                .build());
    }
}
 
Example 22
Source Project: presto   Source File: TestJdbcRecordSet.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testIdempotentClose()
{
    RecordSet recordSet = createRecordSet(ImmutableList.of(
            columnHandles.get("value"),
            columnHandles.get("value"),
            columnHandles.get("text")));

    RecordCursor cursor = recordSet.cursor();
    cursor.close();
    cursor.close();
}
 
Example 23
Source Project: presto   Source File: HiveCoercionRecordCursor.java    License: Apache License 2.0 5 votes vote down vote up
private void assureLoaded(RecordCursor delegate, int field)
{
    if (!loaded) {
        isNull = delegate.isNull(field);
        if (!isNull) {
            coerce(delegate, field);
        }
        loaded = true;
    }
}
 
Example 24
Source Project: presto   Source File: HiveUtil.java    License: Apache License 2.0 5 votes vote down vote up
public static void closeWithSuppression(RecordCursor recordCursor, Throwable throwable)
{
    requireNonNull(recordCursor, "recordCursor is null");
    requireNonNull(throwable, "throwable is null");
    try {
        recordCursor.close();
    }
    catch (RuntimeException e) {
        // Self-suppression not permitted
        if (throwable != e) {
            throwable.addSuppressed(e);
        }
    }
}
 
Example 25
Source Project: presto   Source File: TableStatisticsRecorder.java    License: Apache License 2.0 5 votes vote down vote up
public TableStatisticsData recordStatistics(Table table, double scaleFactor)
{
    Session session = Session.getDefaultSession()
            .withScale(scaleFactor)
            .withParallelism(1)
            .withNoSexism(false);

    List<Column> columns = ImmutableList.copyOf(table.getColumns());
    RecordCursor recordCursor = new TpcdsRecordSet(Results.constructResults(table, session), columns)
            .cursor();

    List<ColumnStatisticsRecorder> statisticsRecorders = createStatisticsRecorders(columns);
    long rowCount = 0;

    while (recordCursor.advanceNextPosition()) {
        rowCount++;
        for (int columnId = 0; columnId < columns.size(); columnId++) {
            Comparable<?> value = getPrestoValue(recordCursor, columns, columnId);
            statisticsRecorders.get(columnId).record(value);
        }
    }

    Map<String, ColumnStatisticsData> columnSampleStatistics = IntStream.range(0, columns.size())
            .boxed()
            .collect(toImmutableMap(
                    i -> columns.get(i).getName(),
                    i -> statisticsRecorders.get(i).getRecording()));
    return new TableStatisticsData(rowCount, columnSampleStatistics);
}
 
Example 26
Source Project: presto   Source File: AppendingRecordSet.java    License: Apache License 2.0 5 votes vote down vote up
private AppendingRecordCursor(RecordCursor delegate, int delegateFieldCount, List<Object> appendedValues, List<Type> appendedTypes)
{
    this.delegate = requireNonNull(delegate, "delegate is null");
    this.delegateFieldCount = delegateFieldCount;
    checkArgument(delegateFieldCount >= 0, "delegateFieldCount must be greater than or equal to zero");
    this.appendedValues = requireNonNull(appendedValues, "appendedValues is null"); // May contain null elements
    this.appendedTypes = ImmutableList.copyOf(requireNonNull(appendedTypes, "appendedTypes is null"));
    checkArgument(appendedValues.size() == appendedTypes.size(), "appendedValues must have the same size as appendedTypes");
}
 
Example 27
Source Project: presto   Source File: ConcatRecordSet.java    License: Apache License 2.0 5 votes vote down vote up
private ConcatRecordCursor(Iterator<RecordCursor> iterator, List<Type> types)
{
    // NOTE: this cursor implementation relies on the fact that the
    // cursor creation in the Iterable is lazy so DO NOT materialize this into
    // an ImmutableList
    this.iterator = requireNonNull(iterator, "iterator is null");
    this.types = ImmutableList.copyOf(requireNonNull(types, "types is null"));
}
 
Example 28
Source Project: presto   Source File: TpchIndexedData.java    License: Apache License 2.0 5 votes vote down vote up
private static List<Object> extractValues(RecordCursor cursor, List<Type> types)
{
    List<Object> list = new ArrayList<>(types.size());
    for (int i = 0; i < types.size(); i++) {
        list.add(extractObject(cursor, i, types.get(i)));
    }
    return list;
}
 
Example 29
Source Project: presto   Source File: CatalogJdbcTable.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public RecordCursor cursor(ConnectorTransactionHandle transactionHandle, ConnectorSession connectorSession, TupleDomain<Integer> constraint)
{
    Session session = ((FullConnectorSession) connectorSession).getSession();
    Builder table = InMemoryRecordSet.builder(METADATA);
    for (String name : listCatalogs(session, metadata, accessControl).keySet()) {
        table.addRow(name);
    }
    return table.build().cursor();
}
 
Example 30
Source Project: presto   Source File: TableJdbcTable.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public RecordCursor cursor(ConnectorTransactionHandle transactionHandle, ConnectorSession connectorSession, TupleDomain<Integer> constraint)
{
    Session session = ((FullConnectorSession) connectorSession).getSession();
    Optional<String> catalogFilter = tryGetSingleVarcharValue(constraint, 0);
    Optional<String> schemaFilter = tryGetSingleVarcharValue(constraint, 1);
    Optional<String> tableFilter = tryGetSingleVarcharValue(constraint, 2);
    Optional<String> typeFilter = tryGetSingleVarcharValue(constraint, 3);

    boolean includeTables = emptyOrEquals(typeFilter, "TABLE");
    boolean includeViews = emptyOrEquals(typeFilter, "VIEW");
    Builder table = InMemoryRecordSet.builder(METADATA);

    if (!includeTables && !includeViews) {
        return table.build().cursor();
    }

    for (String catalog : listCatalogs(session, metadata, accessControl, catalogFilter).keySet()) {
        QualifiedTablePrefix prefix = tablePrefix(catalog, schemaFilter, tableFilter);

        Set<SchemaTableName> views = listViews(session, metadata, accessControl, prefix);
        for (SchemaTableName name : listTables(session, metadata, accessControl, prefix)) {
            boolean isView = views.contains(name);
            if ((includeTables && !isView) || (includeViews && isView)) {
                table.addRow(tableRow(catalog, name, isView ? "VIEW" : "TABLE"));
            }
        }
    }
    return table.build().cursor();
}