Search in sources :

Example 6 with RecordSet

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

the class TestPrometheusRecordSet method testCursorSimple.

@Test
public void testCursorSimple() {
    RecordSet recordSet = new PrometheusRecordSet(new PrometheusClient(new PrometheusConnectorConfig(), METRIC_CODEC, TESTING_TYPE_MANAGER), new PrometheusSplit(dataUri), ImmutableList.of(new PrometheusColumnHandle("labels", varcharMapType, 0), new PrometheusColumnHandle("timestamp", TIMESTAMP_COLUMN_TYPE, 1), new PrometheusColumnHandle("value", DoubleType.DOUBLE, 2)));
    RecordCursor cursor = recordSet.cursor();
    assertEquals(cursor.getType(0), varcharMapType);
    assertEquals(cursor.getType(1), TIMESTAMP_COLUMN_TYPE);
    assertEquals(cursor.getType(2), DoubleType.DOUBLE);
    List<PrometheusStandardizedRow> actual = new ArrayList<>();
    while (cursor.advanceNextPosition()) {
        actual.add(new PrometheusStandardizedRow((Block) cursor.getObject(0), (Instant) cursor.getObject(1), cursor.getDouble(2)));
        assertFalse(cursor.isNull(0));
        assertFalse(cursor.isNull(1));
        assertFalse(cursor.isNull(2));
    }
    List<PrometheusStandardizedRow> expected = ImmutableList.<PrometheusStandardizedRow>builder().add(new PrometheusStandardizedRow(getBlockFromMap(varcharMapType, ImmutableMap.of("instance", "localhost:9090", "__name__", "up", "job", "prometheus")), ofEpochMilli(1565962969044L), 1.0)).add(new PrometheusStandardizedRow(getBlockFromMap(varcharMapType, ImmutableMap.of("instance", "localhost:9090", "__name__", "up", "job", "prometheus")), ofEpochMilli(1565962984045L), 1.0)).add(new PrometheusStandardizedRow(getBlockFromMap(varcharMapType, ImmutableMap.of("instance", "localhost:9090", "__name__", "up", "job", "prometheus")), ofEpochMilli(1565962999044L), 1.0)).add(new PrometheusStandardizedRow(getBlockFromMap(varcharMapType, ImmutableMap.of("instance", "localhost:9090", "__name__", "up", "job", "prometheus")), ofEpochMilli(1565963014044L), 1.0)).build();
    assertThat(actual).as("actual").hasSize(expected.size());
    for (int i = 0; i < actual.size(); i++) {
        PrometheusStandardizedRow actualRow = actual.get(i);
        PrometheusStandardizedRow expectedRow = expected.get(i);
        assertEquals(getMapFromBlock(varcharMapType, actualRow.getLabels()), getMapFromBlock(varcharMapType, expectedRow.getLabels()));
        assertEquals(actualRow.getTimestamp(), expectedRow.getTimestamp());
        assertEquals(actualRow.getValue(), expectedRow.getValue());
    }
}
Also used : RecordCursor(io.trino.spi.connector.RecordCursor) Instant(java.time.Instant) ArrayList(java.util.ArrayList) PrometheusRecordCursor.getMapFromBlock(io.trino.plugin.prometheus.PrometheusRecordCursor.getMapFromBlock) Block(io.trino.spi.block.Block) RecordSet(io.trino.spi.connector.RecordSet) Test(org.testng.annotations.Test)

Example 7 with RecordSet

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

the class TestPrometheusRecordSetProvider method testGetRecordSet.

@Test
public void testGetRecordSet() {
    ConnectorTableHandle tableHandle = new PrometheusTableHandle("schema", "table");
    PrometheusRecordSetProvider recordSetProvider = new PrometheusRecordSetProvider(client);
    RecordSet recordSet = recordSetProvider.getRecordSet(PrometheusTransactionHandle.INSTANCE, SESSION, new PrometheusSplit(dataUri), tableHandle, ImmutableList.of(new PrometheusColumnHandle("labels", varcharMapType, 0), new PrometheusColumnHandle("timestamp", TIMESTAMP_COLUMN_TYPE, 1), new PrometheusColumnHandle("value", DoubleType.DOUBLE, 2)));
    assertNotNull(recordSet, "recordSet is null");
    RecordCursor cursor = recordSet.cursor();
    assertNotNull(cursor, "cursor is null");
    Map<Instant, Map<?, ?>> actual = new LinkedHashMap<>();
    while (cursor.advanceNextPosition()) {
        actual.put((Instant) cursor.getObject(1), getMapFromBlock(varcharMapType, (Block) cursor.getObject(0)));
    }
    Map<Instant, Map<String, String>> expected = ImmutableMap.<Instant, Map<String, String>>builder().put(ofEpochMilli(1565962969044L), ImmutableMap.of("instance", "localhost:9090", "__name__", "up", "job", "prometheus")).put(ofEpochMilli(1565962984045L), ImmutableMap.of("instance", "localhost:9090", "__name__", "up", "job", "prometheus")).put(ofEpochMilli(1565962999044L), ImmutableMap.of("instance", "localhost:9090", "__name__", "up", "job", "prometheus")).put(ofEpochMilli(1565963014044L), ImmutableMap.of("instance", "localhost:9090", "__name__", "up", "job", "prometheus")).buildOrThrow();
    assertEquals(actual, expected);
}
Also used : RecordCursor(io.trino.spi.connector.RecordCursor) Instant(java.time.Instant) ConnectorTableHandle(io.trino.spi.connector.ConnectorTableHandle) LinkedHashMap(java.util.LinkedHashMap) PrometheusRecordCursor.getMapFromBlock(io.trino.plugin.prometheus.PrometheusRecordCursor.getMapFromBlock) Block(io.trino.spi.block.Block) RecordSet(io.trino.spi.connector.RecordSet) ImmutableMap(com.google.common.collect.ImmutableMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Test(org.testng.annotations.Test)

Example 8 with RecordSet

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

the class TestPrometheusIntegrationMetrics method testGetColumnTypes.

@Test
public void testGetColumnTypes() {
    String dataUri = server.getUri().toString();
    RecordSet recordSet = new PrometheusRecordSet(client, new PrometheusSplit(dataUri), ImmutableList.of(new PrometheusColumnHandle("labels", createUnboundedVarcharType(), 0), new PrometheusColumnHandle("value", DoubleType.DOUBLE, 1), new PrometheusColumnHandle("timestamp", TIMESTAMP_COLUMN_TYPE, 2)));
    assertEquals(recordSet.getColumnTypes(), ImmutableList.of(createUnboundedVarcharType(), DoubleType.DOUBLE, TIMESTAMP_COLUMN_TYPE));
    recordSet = new PrometheusRecordSet(client, new PrometheusSplit(dataUri), ImmutableList.of(new PrometheusColumnHandle("value", BIGINT, 1), new PrometheusColumnHandle("text", createUnboundedVarcharType(), 0)));
    assertEquals(recordSet.getColumnTypes(), ImmutableList.of(BIGINT, createUnboundedVarcharType()));
    recordSet = new PrometheusRecordSet(client, new PrometheusSplit(dataUri), ImmutableList.of(new PrometheusColumnHandle("value", BIGINT, 1), new PrometheusColumnHandle("value", BIGINT, 1), new PrometheusColumnHandle("text", createUnboundedVarcharType(), 0)));
    assertEquals(recordSet.getColumnTypes(), ImmutableList.of(BIGINT, BIGINT, createUnboundedVarcharType()));
    recordSet = new PrometheusRecordSet(client, new PrometheusSplit(dataUri), ImmutableList.of());
    assertEquals(recordSet.getColumnTypes(), ImmutableList.of());
}
Also used : RecordSet(io.trino.spi.connector.RecordSet) Test(org.testng.annotations.Test)

Example 9 with RecordSet

use of io.trino.spi.connector.RecordSet 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 10 with RecordSet

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

the class TestJdbcRecordSet method testGetColumnTypes.

@Test
public void testGetColumnTypes() {
    RecordSet recordSet = createRecordSet(ImmutableList.of(new JdbcColumnHandle("text", JDBC_VARCHAR, VARCHAR), new JdbcColumnHandle("text_short", JDBC_VARCHAR, createVarcharType(32)), new JdbcColumnHandle("value", JDBC_BIGINT, BIGINT)));
    assertEquals(recordSet.getColumnTypes(), ImmutableList.of(VARCHAR, createVarcharType(32), BIGINT));
    recordSet = createRecordSet(ImmutableList.of(new JdbcColumnHandle("value", JDBC_BIGINT, BIGINT), new JdbcColumnHandle("text", JDBC_VARCHAR, VARCHAR)));
    assertEquals(recordSet.getColumnTypes(), ImmutableList.of(BIGINT, VARCHAR));
    recordSet = createRecordSet(ImmutableList.of(new JdbcColumnHandle("value", JDBC_BIGINT, BIGINT), new JdbcColumnHandle("value", JDBC_BIGINT, BIGINT), new JdbcColumnHandle("text", JDBC_VARCHAR, VARCHAR)));
    assertEquals(recordSet.getColumnTypes(), ImmutableList.of(BIGINT, BIGINT, VARCHAR));
    recordSet = createRecordSet(ImmutableList.of());
    assertEquals(recordSet.getColumnTypes(), ImmutableList.of());
}
Also used : RecordSet(io.trino.spi.connector.RecordSet) Test(org.testng.annotations.Test)

Aggregations

RecordSet (io.trino.spi.connector.RecordSet)25 Test (org.testng.annotations.Test)14 RecordCursor (io.trino.spi.connector.RecordCursor)12 LinkedHashMap (java.util.LinkedHashMap)8 ConnectorTransactionHandle (io.trino.spi.connector.ConnectorTransactionHandle)6 List (java.util.List)6 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)5 ColumnHandle (io.trino.spi.connector.ColumnHandle)5 Objects.requireNonNull (java.util.Objects.requireNonNull)5 ImmutableSet.toImmutableSet (com.google.common.collect.ImmutableSet.toImmutableSet)4 ConnectorSession (io.trino.spi.connector.ConnectorSession)4 ConnectorTableHandle (io.trino.spi.connector.ConnectorTableHandle)4 ArrayList (java.util.ArrayList)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 ImmutableMap (com.google.common.collect.ImmutableMap)3 DispatchingRowDecoderFactory (io.trino.decoder.DispatchingRowDecoderFactory)3 RowDecoder (io.trino.decoder.RowDecoder)3 ConnectorRecordSetProvider (io.trino.spi.connector.ConnectorRecordSetProvider)3