Search in sources :

Example 11 with RecordCursor

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

the class TableStatisticsRecorder method recordStatistics.

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 = getTrinoValue(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);
}
Also used : IntStream(java.util.stream.IntStream) RecordCursor(io.trino.spi.connector.RecordCursor) Session(io.trino.tpcds.Session) ColumnType(io.trino.tpcds.column.ColumnType) TpcdsRecordSet(io.trino.plugin.tpcds.TpcdsRecordSet) Results(io.trino.tpcds.Results) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) String.format(java.lang.String.format) List(java.util.List) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) ImmutableList(com.google.common.collect.ImmutableList) Column(io.trino.tpcds.column.Column) Map(java.util.Map) Table(io.trino.tpcds.Table) RecordCursor(io.trino.spi.connector.RecordCursor) TpcdsRecordSet(io.trino.plugin.tpcds.TpcdsRecordSet) Column(io.trino.tpcds.column.Column) Session(io.trino.tpcds.Session)

Example 12 with RecordCursor

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

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, toIntExact(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) PreparedBatch(org.jdbi.v3.core.statement.PreparedBatch) TpchRecordSet.createTpchRecordSet(io.trino.plugin.tpch.TpchRecordSet.createTpchRecordSet) UNKNOWN(io.trino.type.UnknownType.UNKNOWN) Array(java.sql.Array) CUSTOMER(io.trino.tpch.TpchTable.CUSTOMER) BigDecimal(java.math.BigDecimal) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) TpchTableHandle(io.trino.plugin.tpch.TpchTableHandle) ParsedSql(org.jdbi.v3.core.statement.ParsedSql) Handle(org.jdbi.v3.core.Handle) LocalTime(java.time.LocalTime) Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) TIMESTAMP_WITH_TIME_ZONE(io.trino.spi.type.TimestampWithTimeZoneType.TIMESTAMP_WITH_TIME_ZONE) INTEGER(io.trino.spi.type.IntegerType.INTEGER) UUID(io.trino.spi.type.UuidType.UUID) SMALLINT(io.trino.spi.type.SmallintType.SMALLINT) TpchTable(io.trino.tpch.TpchTable) PART(io.trino.tpch.TpchTable.PART) MathContext(java.math.MathContext) Collections.nCopies(java.util.Collections.nCopies) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) REGION(io.trino.tpch.TpchTable.REGION) ArrayType(io.trino.spi.type.ArrayType) SchemaTableName(io.trino.spi.connector.SchemaTableName) String.format(java.lang.String.format) StatementContext(org.jdbi.v3.core.statement.StatementContext) Preconditions.checkState(com.google.common.base.Preconditions.checkState) List(java.util.List) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) BIGINT(io.trino.spi.type.BigintType.BIGINT) LocalDate(java.time.LocalDate) RecordSet(io.trino.spi.connector.RecordSet) Optional(java.util.Optional) DecimalType(io.trino.spi.type.DecimalType) DATE(io.trino.spi.type.DateType.DATE) REAL(io.trino.spi.type.RealType.REAL) LINE_ITEM(io.trino.tpch.TpchTable.LINE_ITEM) Joiner(com.google.common.base.Joiner) Session(io.trino.Session) NATION(io.trino.tpch.TpchTable.NATION) TimeType(io.trino.spi.type.TimeType) ColumnMetadata(io.trino.spi.connector.ColumnMetadata) Type(io.trino.spi.type.Type) LocalDateTime(java.time.LocalDateTime) BOOLEAN(io.trino.spi.type.BooleanType.BOOLEAN) ConnectorTableMetadata(io.trino.spi.connector.ConnectorTableMetadata) JsonFunctions.jsonParse(io.trino.operator.scalar.JsonFunctions.jsonParse) TimestampType(io.trino.spi.type.TimestampType) ORDERS(io.trino.tpch.TpchTable.ORDERS) ArrayList(java.util.ArrayList) VarcharType(io.trino.spi.type.VarcharType) TIME_WITH_TIME_ZONE(io.trino.spi.type.TimeWithTimeZoneType.TIME_WITH_TIME_ZONE) SQLException(java.sql.SQLException) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Chars.padSpaces(io.trino.spi.type.Chars.padSpaces) VARBINARY(io.trino.spi.type.VarbinaryType.VARBINARY) Math.toIntExact(java.lang.Math.toIntExact) RowMapper(org.jdbi.v3.core.mapper.RowMapper) Jdbi(org.jdbi.v3.core.Jdbi) TINY_SCHEMA_NAME(io.trino.plugin.tpch.TpchMetadata.TINY_SCHEMA_NAME) RecordCursor(io.trino.spi.connector.RecordCursor) Language(org.intellij.lang.annotations.Language) Date(java.sql.Date) TimeUnit(java.util.concurrent.TimeUnit) DOUBLE(io.trino.spi.type.DoubleType.DOUBLE) CharType(io.trino.spi.type.CharType) Closeable(java.io.Closeable) TpchMetadata(io.trino.plugin.tpch.TpchMetadata) SqlParser(org.jdbi.v3.core.statement.SqlParser) TINYINT(io.trino.spi.type.TinyintType.TINYINT) JSON(io.trino.type.JsonType.JSON) ColumnMetadata(io.trino.spi.connector.ColumnMetadata) ArrayType(io.trino.spi.type.ArrayType) DecimalType(io.trino.spi.type.DecimalType) TimeType(io.trino.spi.type.TimeType) Type(io.trino.spi.type.Type) TimestampType(io.trino.spi.type.TimestampType) VarcharType(io.trino.spi.type.VarcharType) CharType(io.trino.spi.type.CharType) RecordCursor(io.trino.spi.connector.RecordCursor) VarcharType(io.trino.spi.type.VarcharType) PreparedBatch(org.jdbi.v3.core.statement.PreparedBatch) LocalDate(java.time.LocalDate) Date(java.sql.Date)

Example 13 with RecordCursor

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

the class TestListBasedRecordSet method testCursor.

@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);
    assertThatThrownBy(() -> cursor.getLong(2)).isInstanceOf(IndexOutOfBoundsException.class).hasMessage("Index 2 out of bounds for length 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());
    assertThatThrownBy(() -> cursor.getLong(0)).isInstanceOf(IndexOutOfBoundsException.class).hasMessage("Index 3 out of bounds for length 3");
}
Also used : RecordCursor(io.trino.spi.connector.RecordCursor) Test(org.testng.annotations.Test)

Example 14 with RecordCursor

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

the class TestListBasedRecordSet method testEmptyCursor.

@Test
public void testEmptyCursor() {
    ListBasedRecordSet recordSet = new ListBasedRecordSet(ImmutableList.of(), ImmutableList.of(BIGINT, INTEGER));
    assertEquals(recordSet.getColumnTypes(), ImmutableList.of(BIGINT, INTEGER));
    RecordCursor cursor = recordSet.cursor();
    assertFalse(cursor.advanceNextPosition());
}
Also used : RecordCursor(io.trino.spi.connector.RecordCursor) Test(org.testng.annotations.Test)

Example 15 with RecordCursor

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

the class TrinoThriftTypeUtils method fromIntBasedColumn.

public static TrinoThriftBlock fromIntBasedColumn(RecordSet recordSet, int columnIndex, int positions, BiFunction<boolean[], int[], TrinoThriftBlock> 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);
}
Also used : RecordCursor(io.trino.spi.connector.RecordCursor)

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