Search in sources :

Example 1 with RecordSet

use of io.prestosql.spi.connector.RecordSet in project hetu-core by openlookeng.

the class TpchIndexedData method indexTable.

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());
}
Also used : Iterables(com.google.common.collect.Iterables) TpchTableHandle(io.prestosql.plugin.tpch.TpchTableHandle) Slice(io.airlift.slice.Slice) ListMultimap(com.google.common.collect.ListMultimap) RecordSet(io.prestosql.spi.connector.RecordSet) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) SchemaTableName(io.prestosql.spi.connector.SchemaTableName) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) TpchRecordSetProvider(io.prestosql.plugin.tpch.TpchRecordSetProvider) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) RecordCursor(io.prestosql.spi.connector.RecordCursor) Preconditions.checkPositionIndex(com.google.common.base.Preconditions.checkPositionIndex) Type(io.prestosql.spi.type.Type) ImmutableMap(com.google.common.collect.ImmutableMap) TupleDomain(io.prestosql.spi.predicate.TupleDomain) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) AbstractIterator(com.google.common.collect.AbstractIterator) Set(java.util.Set) Preconditions.checkState(com.google.common.base.Preconditions.checkState) TpchTable(io.airlift.tpch.TpchTable) Objects(java.util.Objects) List(java.util.List) TpchMetadata(io.prestosql.plugin.tpch.TpchMetadata) ColumnHandle(io.prestosql.spi.connector.ColumnHandle) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap) Optional(java.util.Optional) Type(io.prestosql.spi.type.Type) RecordCursor(io.prestosql.spi.connector.RecordCursor) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap)

Example 2 with RecordSet

use of io.prestosql.spi.connector.RecordSet in project hetu-core by openlookeng.

the class H2QueryRunner method insertRows.

private static void insertRows(ConnectorTableMetadata tableMetadata, Handle handle, RecordSet data) {
    List<ColumnMetadata> columns = tableMetadata.getColumns().stream().filter(columnMetadata -> !columnMetadata.isHidden()).collect(toImmutableList());
    String vars = Joiner.on(',').join(nCopies(columns.size(), "?"));
    String sql = format("INSERT INTO %s VALUES (%s)", tableMetadata.getTable().getTableName(), vars);
    RecordCursor cursor = data.cursor();
    while (true) {
        // insert 1000 rows at a time
        PreparedBatch batch = handle.prepareBatch(sql);
        for (int row = 0; row < 1000; row++) {
            if (!cursor.advanceNextPosition()) {
                if (batch.size() > 0) {
                    batch.execute();
                }
                return;
            }
            for (int column = 0; column < columns.size(); column++) {
                Type type = columns.get(column).getType();
                if (BOOLEAN.equals(type)) {
                    batch.bind(column, cursor.getBoolean(column));
                } else if (BIGINT.equals(type)) {
                    batch.bind(column, cursor.getLong(column));
                } else if (INTEGER.equals(type)) {
                    batch.bind(column, (int) cursor.getLong(column));
                } else if (DOUBLE.equals(type)) {
                    batch.bind(column, cursor.getDouble(column));
                } else if (type instanceof VarcharType) {
                    batch.bind(column, cursor.getSlice(column).toStringUtf8());
                } else if (DATE.equals(type)) {
                    long millisUtc = TimeUnit.DAYS.toMillis(cursor.getLong(column));
                    // H2 expects dates in to be millis at midnight in the JVM timezone
                    long localMillis = DateTimeZone.UTC.getMillisKeepLocal(DateTimeZone.getDefault(), millisUtc);
                    batch.bind(column, new Date(localMillis));
                } else {
                    throw new IllegalArgumentException("Unsupported type " + type);
                }
            }
            batch.add();
        }
        batch.execute();
    }
}
Also used : DateTimeZone(org.joda.time.DateTimeZone) Varchars.isVarcharType(io.prestosql.spi.type.Varchars.isVarcharType) PreparedBatch(org.jdbi.v3.core.statement.PreparedBatch) LINE_ITEM(io.airlift.tpch.TpchTable.LINE_ITEM) DecimalType(io.prestosql.spi.type.DecimalType) RecordSet(io.prestosql.spi.connector.RecordSet) MaterializedResult(io.prestosql.testing.MaterializedResult) Array(java.sql.Array) BigDecimal(java.math.BigDecimal) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) ParsedSql(org.jdbi.v3.core.statement.ParsedSql) ResultSet(java.sql.ResultSet) Handle(org.jdbi.v3.core.Handle) LocalTime(java.time.LocalTime) BOOLEAN(io.prestosql.spi.type.BooleanType.BOOLEAN) Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) JSON(io.prestosql.type.JsonType.JSON) Type(io.prestosql.spi.type.Type) CUSTOMER(io.airlift.tpch.TpchTable.CUSTOMER) REGION(io.airlift.tpch.TpchTable.REGION) BIGINT(io.prestosql.spi.type.BigintType.BIGINT) Chars.isCharType(io.prestosql.spi.type.Chars.isCharType) JsonFunctions.jsonParse(io.prestosql.operator.scalar.JsonFunctions.jsonParse) MathContext(java.math.MathContext) Collections.nCopies(java.util.Collections.nCopies) ArrayType(io.prestosql.spi.type.ArrayType) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) TIME(io.prestosql.spi.type.TimeType.TIME) TIMESTAMP(io.prestosql.spi.type.TimestampType.TIMESTAMP) TINYINT(io.prestosql.spi.type.TinyintType.TINYINT) String.format(java.lang.String.format) StatementContext(org.jdbi.v3.core.statement.StatementContext) Preconditions.checkState(com.google.common.base.Preconditions.checkState) TpchTable(io.airlift.tpch.TpchTable) List(java.util.List) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) LocalDate(java.time.LocalDate) Optional(java.util.Optional) TIMESTAMP_WITH_TIME_ZONE(io.prestosql.spi.type.TimestampWithTimeZoneType.TIMESTAMP_WITH_TIME_ZONE) TpchRecordSet.createTpchRecordSet(io.prestosql.plugin.tpch.TpchRecordSet.createTpchRecordSet) UNKNOWN(io.prestosql.spi.type.UnknownType.UNKNOWN) Joiner(com.google.common.base.Joiner) TpchTableHandle(io.prestosql.plugin.tpch.TpchTableHandle) Strings.padEnd(com.google.common.base.Strings.padEnd) NATION(io.airlift.tpch.TpchTable.NATION) TIME_WITH_TIME_ZONE(io.prestosql.spi.type.TimeWithTimeZoneType.TIME_WITH_TIME_ZONE) ORDERS(io.airlift.tpch.TpchTable.ORDERS) LocalDateTime(java.time.LocalDateTime) CharType(io.prestosql.spi.type.CharType) INTEGER(io.prestosql.spi.type.IntegerType.INTEGER) PART(io.airlift.tpch.TpchTable.PART) ArrayList(java.util.ArrayList) SchemaTableName(io.prestosql.spi.connector.SchemaTableName) SQLException(java.sql.SQLException) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Session(io.prestosql.Session) DOUBLE(io.prestosql.spi.type.DoubleType.DOUBLE) RecordCursor(io.prestosql.spi.connector.RecordCursor) DATE(io.prestosql.spi.type.DateType.DATE) REAL(io.prestosql.spi.type.RealType.REAL) RowMapper(org.jdbi.v3.core.mapper.RowMapper) Jdbi(org.jdbi.v3.core.Jdbi) Language(org.intellij.lang.annotations.Language) TINY_SCHEMA_NAME(io.prestosql.plugin.tpch.TpchMetadata.TINY_SCHEMA_NAME) ColumnMetadata(io.prestosql.spi.connector.ColumnMetadata) MaterializedRow(io.prestosql.testing.MaterializedRow) ConnectorTableMetadata(io.prestosql.spi.connector.ConnectorTableMetadata) Date(java.sql.Date) TimeUnit(java.util.concurrent.TimeUnit) TpchMetadata(io.prestosql.plugin.tpch.TpchMetadata) SMALLINT(io.prestosql.spi.type.SmallintType.SMALLINT) Closeable(java.io.Closeable) SqlParser(org.jdbi.v3.core.statement.SqlParser) VarcharType(io.prestosql.spi.type.VarcharType) ColumnMetadata(io.prestosql.spi.connector.ColumnMetadata) Varchars.isVarcharType(io.prestosql.spi.type.Varchars.isVarcharType) DecimalType(io.prestosql.spi.type.DecimalType) Type(io.prestosql.spi.type.Type) Chars.isCharType(io.prestosql.spi.type.Chars.isCharType) ArrayType(io.prestosql.spi.type.ArrayType) CharType(io.prestosql.spi.type.CharType) VarcharType(io.prestosql.spi.type.VarcharType) RecordCursor(io.prestosql.spi.connector.RecordCursor) Varchars.isVarcharType(io.prestosql.spi.type.Varchars.isVarcharType) VarcharType(io.prestosql.spi.type.VarcharType) PreparedBatch(org.jdbi.v3.core.statement.PreparedBatch) LocalDate(java.time.LocalDate) Date(java.sql.Date)

Example 3 with RecordSet

use of io.prestosql.spi.connector.RecordSet in project hetu-core by openlookeng.

the class TestHBase method testGetSRecordSet.

/**
 * testGetSRecordSet
 */
@Test
public void testGetSRecordSet() {
    List<HostAddress> hostAddressList = new ArrayList<>(1);
    Map<Integer, List<Range>> ranges = new HashMap<>();
    HBaseSplit split = new HBaseSplit("rowkey", TestUtils.createHBaseTableHandle(), hostAddressList, null, null, ranges, 0, false, null);
    HBaseRecordSetProvider hrsp = new HBaseRecordSetProvider(hconn);
    RecordSet rs = hrsp.getRecordSet(new HBaseTransactionHandle(), session, split, TestUtils.createHBaseTableHandle(), hconn.getTable("hbase.test_table").getColumns());
    assertEquals(5, rs.getColumnTypes().size());
}
Also used : HashMap(java.util.HashMap) HBaseRecordSetProvider(io.hetu.core.plugin.hbase.query.HBaseRecordSetProvider) ArrayList(java.util.ArrayList) HBaseTransactionHandle(io.hetu.core.plugin.hbase.connector.HBaseTransactionHandle) List(java.util.List) ArrayList(java.util.ArrayList) HBaseSplit(io.hetu.core.plugin.hbase.split.HBaseSplit) RecordSet(io.prestosql.spi.connector.RecordSet) HostAddress(io.prestosql.spi.HostAddress) Test(org.testng.annotations.Test)

Example 4 with RecordSet

use of io.prestosql.spi.connector.RecordSet in project hetu-core by openlookeng.

the class HBasePageSourceProvider method createPageSource.

@Override
public ConnectorPageSource createPageSource(ConnectorTransactionHandle transactionHandle, ConnectorSession session, ConnectorSplit split, ConnectorTableHandle table, List<ColumnHandle> columns) {
    // if delete rows, we should replace $rowId -> real rowkey name
    List<ColumnHandle> columnsReplaceRowKey = new ArrayList<>();
    columns.forEach(ch -> {
        if (Constants.HBASE_ROWID_NAME.equals(ch.getColumnName())) {
            if (ch instanceof HBaseColumnHandle && table instanceof HBaseTableHandle) {
                HBaseColumnHandle rowColumnHandle = (HBaseColumnHandle) ch;
                columnsReplaceRowKey.add(new HBaseColumnHandle(((HBaseTableHandle) table).getRowId(), rowColumnHandle.getFamily(), rowColumnHandle.getQualifier(), rowColumnHandle.getType(), rowColumnHandle.getOrdinal(), rowColumnHandle.getComment(), rowColumnHandle.isIndexed()));
            }
        } else {
            columnsReplaceRowKey.add(ch);
        }
    });
    RecordSet recordSet = recordSetProvider.getRecordSet(transactionHandle, session, split, table, columnsReplaceRowKey);
    HBaseRecordSet hbaseRecordSet = null;
    if (recordSet instanceof HBaseRecordSet) {
        hbaseRecordSet = (HBaseRecordSet) recordSet;
    }
    if (columnsReplaceRowKey.stream().anyMatch(ch -> (ch instanceof HBaseColumnHandle) && (table instanceof HBaseTableHandle) && ((HBaseColumnHandle) ch).getOrdinal() == ((HBaseTableHandle) table).getRowIdOrdinal())) {
        return new HBaseUpdatablePageSource(hbaseRecordSet, hbaseConnection);
    } else {
        return new RecordPageSource(recordSet);
    }
}
Also used : HBaseColumnHandle(io.hetu.core.plugin.hbase.connector.HBaseColumnHandle) ColumnHandle(io.prestosql.spi.connector.ColumnHandle) HBaseColumnHandle(io.hetu.core.plugin.hbase.connector.HBaseColumnHandle) ArrayList(java.util.ArrayList) RecordSet(io.prestosql.spi.connector.RecordSet) HBaseTableHandle(io.hetu.core.plugin.hbase.connector.HBaseTableHandle) RecordPageSource(io.prestosql.spi.connector.RecordPageSource)

Example 5 with RecordSet

use of io.prestosql.spi.connector.RecordSet in project hetu-core by openlookeng.

the class TestJdbcRecordSet method testCursorSimple.

@Test
public void testCursorSimple() {
    RecordSet recordSet = new JdbcRecordSet(jdbcClient, SESSION, split, table, 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());
    }
}
Also used : RecordCursor(io.prestosql.spi.connector.RecordCursor) RecordSet(io.prestosql.spi.connector.RecordSet) LinkedHashMap(java.util.LinkedHashMap) Test(org.testng.annotations.Test)

Aggregations

RecordSet (io.prestosql.spi.connector.RecordSet)23 Test (org.testng.annotations.Test)12 RecordCursor (io.prestosql.spi.connector.RecordCursor)10 LinkedHashMap (java.util.LinkedHashMap)7 List (java.util.List)6 ColumnHandle (io.prestosql.spi.connector.ColumnHandle)4 ConnectorTransactionHandle (io.prestosql.spi.connector.ConnectorTransactionHandle)4 Type (io.prestosql.spi.type.Type)4 ArrayList (java.util.ArrayList)4 Optional (java.util.Optional)4 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)3 Preconditions.checkState (com.google.common.base.Preconditions.checkState)3 ImmutableList (com.google.common.collect.ImmutableList)3 RecordPageSource (io.prestosql.spi.connector.RecordPageSource)3 SchemaTableName (io.prestosql.spi.connector.SchemaTableName)3 MappedRecordSet (io.prestosql.split.MappedRecordSet)3 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 ImmutableSet.toImmutableSet (com.google.common.collect.ImmutableSet.toImmutableSet)2 TpchTable (io.airlift.tpch.TpchTable)2