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();
}
}
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));
}
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);
}
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()));
}
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();
}
Aggregations