Search in sources :

Example 76 with ColumnMetadata

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

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

the class IcebergMetadata method getTableMetadata.

/**
 * @throws TableNotFoundException when table cannot be found
 */
private ConnectorTableMetadata getTableMetadata(ConnectorSession session, SchemaTableName table) {
    Table icebergTable = catalog.loadTable(session, table);
    List<ColumnMetadata> columns = getColumnMetadatas(icebergTable);
    ImmutableMap.Builder<String, Object> properties = ImmutableMap.builder();
    properties.put(FILE_FORMAT_PROPERTY, getFileFormat(icebergTable));
    if (!icebergTable.spec().fields().isEmpty()) {
        properties.put(PARTITIONING_PROPERTY, toPartitionFields(icebergTable.spec()));
    }
    if (!icebergTable.location().isEmpty()) {
        properties.put(LOCATION_PROPERTY, icebergTable.location());
    }
    return new ConnectorTableMetadata(table, columns, properties.buildOrThrow(), getTableComment(icebergTable));
}
Also used : ColumnMetadata(io.trino.spi.connector.ColumnMetadata) Table(org.apache.iceberg.Table) ClassLoaderSafeSystemTable(io.trino.plugin.base.classloader.ClassLoaderSafeSystemTable) SystemTable(io.trino.spi.connector.SystemTable) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) ImmutableMap(com.google.common.collect.ImmutableMap) ConnectorTableMetadata(io.trino.spi.connector.ConnectorTableMetadata)

Example 78 with ColumnMetadata

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

the class TestKafkaInternalFieldManager method testInternalField.

@Test
public void testInternalField() {
    KafkaInternalFieldManager.InternalField internalField = new KafkaInternalFieldManager.InternalField(PARTITION_ID_FIELD, "Partition Id", BigintType.BIGINT);
    KafkaColumnHandle kafkaColumnHandle = new KafkaColumnHandle(PARTITION_ID_FIELD, BigintType.BIGINT, null, null, null, false, false, true);
    ColumnMetadata columnMetadata = ColumnMetadata.builder().setName(PARTITION_ID_FIELD).setType(BigintType.BIGINT).setComment(Optional.of("Partition Id")).setHidden(false).build();
    assertThat(internalField.getColumnName()).isEqualTo(PARTITION_ID_FIELD);
    assertThat(internalField.getColumnHandle(0, false)).isEqualTo(kafkaColumnHandle);
    assertThat(internalField.getColumnMetadata(false)).isEqualTo(columnMetadata);
}
Also used : ColumnMetadata(io.trino.spi.connector.ColumnMetadata) Test(org.testng.annotations.Test)

Example 79 with ColumnMetadata

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

the class MemoryMetadata method beginCreateTable.

@Override
public synchronized MemoryOutputTableHandle beginCreateTable(ConnectorSession session, ConnectorTableMetadata tableMetadata, Optional<ConnectorTableLayout> layout, RetryMode retryMode) {
    checkSchemaExists(tableMetadata.getTable().getSchemaName());
    checkTableNotExists(tableMetadata.getTable());
    long tableId = nextTableId.getAndIncrement();
    Set<Node> nodes = nodeManager.getRequiredWorkerNodes();
    checkState(!nodes.isEmpty(), "No Memory nodes available");
    ImmutableList.Builder<ColumnInfo> columns = ImmutableList.builder();
    for (int i = 0; i < tableMetadata.getColumns().size(); i++) {
        ColumnMetadata column = tableMetadata.getColumns().get(i);
        columns.add(new ColumnInfo(new MemoryColumnHandle(i), column.getName(), column.getType()));
    }
    tableIds.put(tableMetadata.getTable(), tableId);
    tables.put(tableId, new TableInfo(tableId, tableMetadata.getTable().getSchemaName(), tableMetadata.getTable().getTableName(), columns.build(), new HashMap<>()));
    return new MemoryOutputTableHandle(tableId, ImmutableSet.copyOf(tableIds.values()));
}
Also used : ColumnMetadata(io.trino.spi.connector.ColumnMetadata) HashMap(java.util.HashMap) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) Node(io.trino.spi.Node) Constraint(io.trino.spi.connector.Constraint)

Example 80 with ColumnMetadata

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

the class LocalFileMetadata method getColumnHandles.

private Map<String, ColumnHandle> getColumnHandles(LocalFileTableHandle tableHandle) {
    ImmutableMap.Builder<String, ColumnHandle> columnHandles = ImmutableMap.builder();
    int index = 0;
    for (ColumnMetadata column : localFileTables.getColumns(tableHandle)) {
        int ordinalPosition;
        if (column.getName().equals(SERVER_ADDRESS_COLUMN_NAME)) {
            ordinalPosition = SERVER_ADDRESS_ORDINAL_POSITION;
        } else {
            ordinalPosition = index;
            index++;
        }
        columnHandles.put(column.getName(), new LocalFileColumnHandle(column.getName(), column.getType(), ordinalPosition));
    }
    return columnHandles.buildOrThrow();
}
Also used : ColumnHandle(io.trino.spi.connector.ColumnHandle) ColumnMetadata(io.trino.spi.connector.ColumnMetadata) ImmutableMap(com.google.common.collect.ImmutableMap) Constraint(io.trino.spi.connector.Constraint)

Aggregations

ColumnMetadata (io.trino.spi.connector.ColumnMetadata)154 SchemaTableName (io.trino.spi.connector.SchemaTableName)75 ConnectorTableMetadata (io.trino.spi.connector.ConnectorTableMetadata)73 Test (org.testng.annotations.Test)64 ImmutableList (com.google.common.collect.ImmutableList)63 ImmutableMap (com.google.common.collect.ImmutableMap)55 List (java.util.List)45 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)43 Optional (java.util.Optional)43 ConnectorSession (io.trino.spi.connector.ConnectorSession)41 TrinoException (io.trino.spi.TrinoException)38 ColumnHandle (io.trino.spi.connector.ColumnHandle)38 Map (java.util.Map)38 Type (io.trino.spi.type.Type)35 Constraint (io.trino.spi.connector.Constraint)32 ConnectorTableHandle (io.trino.spi.connector.ConnectorTableHandle)31 ConnectorMetadata (io.trino.spi.connector.ConnectorMetadata)30 BIGINT (io.trino.spi.type.BigintType.BIGINT)30 HashMap (java.util.HashMap)27 Set (java.util.Set)27