use of com.facebook.presto.spi.ColumnMetadata in project presto by prestodb.
the class TableMetadataSystemTable method buildPages.
private static List<Page> buildPages(MetadataDao dao, ConnectorTableMetadata tableMetadata, TupleDomain<Integer> tupleDomain) {
Map<Integer, NullableValue> domainValues = extractFixedValues(tupleDomain).orElse(ImmutableMap.of());
String schemaName = getStringValue(domainValues.get(getColumnIndex(tableMetadata, SCHEMA_NAME)));
String tableName = getStringValue(domainValues.get(getColumnIndex(tableMetadata, TABLE_NAME)));
PageListBuilder pageBuilder = new PageListBuilder(tableMetadata.getColumns().stream().map(ColumnMetadata::getType).collect(toList()));
List<TableMetadataRow> tableRows = dao.getTableMetadataRows(schemaName, tableName);
PeekingIterator<ColumnMetadataRow> columnRowIterator = peekingIterator(dao.getColumnMetadataRows(schemaName, tableName).iterator());
for (TableMetadataRow tableRow : tableRows) {
while (columnRowIterator.hasNext() && columnRowIterator.peek().getTableId() < tableRow.getTableId()) {
columnRowIterator.next();
}
String temporalColumnName = null;
SortedMap<Integer, String> sortColumnNames = new TreeMap<>();
SortedMap<Integer, String> bucketColumnNames = new TreeMap<>();
OptionalLong temporalColumnId = tableRow.getTemporalColumnId();
while (columnRowIterator.hasNext() && columnRowIterator.peek().getTableId() == tableRow.getTableId()) {
ColumnMetadataRow columnRow = columnRowIterator.next();
if (temporalColumnId.isPresent() && columnRow.getColumnId() == temporalColumnId.getAsLong()) {
temporalColumnName = columnRow.getColumnName();
}
OptionalInt sortOrdinalPosition = columnRow.getSortOrdinalPosition();
if (sortOrdinalPosition.isPresent()) {
sortColumnNames.put(sortOrdinalPosition.getAsInt(), columnRow.getColumnName());
}
OptionalInt bucketOrdinalPosition = columnRow.getBucketOrdinalPosition();
if (bucketOrdinalPosition.isPresent()) {
bucketColumnNames.put(bucketOrdinalPosition.getAsInt(), columnRow.getColumnName());
}
}
pageBuilder.beginRow();
// schema_name, table_name
VARCHAR.writeSlice(pageBuilder.nextBlockBuilder(), utf8Slice(tableRow.getSchemaName()));
VARCHAR.writeSlice(pageBuilder.nextBlockBuilder(), utf8Slice(tableRow.getTableName()));
// temporal_column
if (temporalColumnId.isPresent()) {
if (temporalColumnName == null) {
throw new PrestoException(RAPTOR_CORRUPT_METADATA, format("Table ID %s has corrupt metadata (invalid temporal column ID)", tableRow.getTableId()));
}
VARCHAR.writeSlice(pageBuilder.nextBlockBuilder(), utf8Slice(temporalColumnName));
} else {
pageBuilder.nextBlockBuilder().appendNull();
}
// ordering_columns
writeArray(pageBuilder.nextBlockBuilder(), sortColumnNames.values());
// distribution_name
Optional<String> distributionName = tableRow.getDistributionName();
if (distributionName.isPresent()) {
VARCHAR.writeSlice(pageBuilder.nextBlockBuilder(), utf8Slice(distributionName.get()));
} else {
pageBuilder.nextBlockBuilder().appendNull();
}
// bucket_count
OptionalInt bucketCount = tableRow.getBucketCount();
if (bucketCount.isPresent()) {
BIGINT.writeLong(pageBuilder.nextBlockBuilder(), bucketCount.getAsInt());
} else {
pageBuilder.nextBlockBuilder().appendNull();
}
// bucketing_columns
writeArray(pageBuilder.nextBlockBuilder(), bucketColumnNames.values());
// organized
BOOLEAN.writeBoolean(pageBuilder.nextBlockBuilder(), tableRow.isOrganized());
}
return pageBuilder.build();
}
use of com.facebook.presto.spi.ColumnMetadata in project presto by prestodb.
the class HiveMetadata method getNewTableLayout.
@Override
public Optional<ConnectorNewTableLayout> getNewTableLayout(ConnectorSession session, ConnectorTableMetadata tableMetadata) {
Optional<HiveBucketProperty> bucketProperty = getBucketProperty(tableMetadata.getProperties());
if (!bucketProperty.isPresent()) {
return Optional.empty();
}
if (!bucketWritingEnabled) {
throw new PrestoException(NOT_SUPPORTED, "Writing to bucketed Hive table has been temporarily disabled");
}
List<String> bucketedBy = bucketProperty.get().getBucketedBy();
Map<String, HiveType> hiveTypeMap = tableMetadata.getColumns().stream().collect(toMap(ColumnMetadata::getName, column -> toHiveType(typeTranslator, column.getType())));
return Optional.of(new ConnectorNewTableLayout(new HivePartitioningHandle(connectorId, bucketProperty.get().getBucketCount(), bucketedBy.stream().map(hiveTypeMap::get).collect(toList())), bucketedBy));
}
use of com.facebook.presto.spi.ColumnMetadata in project presto by prestodb.
the class HiveMetadata method columnMetadataGetter.
private static Function<HiveColumnHandle, ColumnMetadata> columnMetadataGetter(Table table, TypeManager typeManager) {
ImmutableList.Builder<String> columnNames = ImmutableList.builder();
table.getPartitionColumns().stream().map(Column::getName).forEach(columnNames::add);
table.getDataColumns().stream().map(Column::getName).forEach(columnNames::add);
List<String> allColumnNames = columnNames.build();
if (allColumnNames.size() > Sets.newHashSet(allColumnNames).size()) {
throw new PrestoException(HIVE_INVALID_METADATA, format("Hive metadata for table %s is invalid: Table descriptor contains duplicate columns", table.getTableName()));
}
List<Column> tableColumns = table.getDataColumns();
ImmutableMap.Builder<String, Optional<String>> builder = ImmutableMap.builder();
for (Column field : concat(tableColumns, table.getPartitionColumns())) {
if ((field.getComment() != null) && !Optional.of("from deserializer").equals(field.getComment())) {
builder.put(field.getName(), field.getComment());
} else {
builder.put(field.getName(), Optional.empty());
}
}
// add hidden columns
builder.put(PATH_COLUMN_NAME, Optional.empty());
if (table.getStorage().getBucketProperty().isPresent()) {
builder.put(BUCKET_COLUMN_NAME, Optional.empty());
}
Map<String, Optional<String>> columnComment = builder.build();
return handle -> new ColumnMetadata(handle.getName(), typeManager.getType(handle.getTypeSignature()), columnComment.get(handle.getName()).orElse(null), columnExtraInfo(handle.isPartitionKey()), handle.isHidden());
}
use of com.facebook.presto.spi.ColumnMetadata in project presto by prestodb.
the class BaseJdbcClient method beginWriteTable.
private JdbcOutputTableHandle beginWriteTable(ConnectorTableMetadata tableMetadata) {
SchemaTableName schemaTableName = tableMetadata.getTable();
String schema = schemaTableName.getSchemaName();
String table = schemaTableName.getTableName();
if (!getSchemaNames().contains(schema)) {
throw new PrestoException(NOT_FOUND, "Schema not found: " + schema);
}
try (Connection connection = driver.connect(connectionUrl, connectionProperties)) {
boolean uppercase = connection.getMetaData().storesUpperCaseIdentifiers();
if (uppercase) {
schema = schema.toUpperCase(ENGLISH);
table = table.toUpperCase(ENGLISH);
}
String catalog = connection.getCatalog();
String temporaryName = "tmp_presto_" + UUID.randomUUID().toString().replace("-", "");
StringBuilder sql = new StringBuilder().append("CREATE TABLE ").append(quoted(catalog, schema, temporaryName)).append(" (");
ImmutableList.Builder<String> columnNames = ImmutableList.builder();
ImmutableList.Builder<Type> columnTypes = ImmutableList.builder();
ImmutableList.Builder<String> columnList = ImmutableList.builder();
for (ColumnMetadata column : tableMetadata.getColumns()) {
String columnName = column.getName();
if (uppercase) {
columnName = columnName.toUpperCase(ENGLISH);
}
columnNames.add(columnName);
columnTypes.add(column.getType());
columnList.add(new StringBuilder().append(quoted(columnName)).append(" ").append(toSqlType(column.getType())).toString());
}
Joiner.on(", ").appendTo(sql, columnList.build());
sql.append(")");
execute(connection, sql.toString());
return new JdbcOutputTableHandle(connectorId, catalog, schema, table, columnNames.build(), columnTypes.build(), temporaryName, connectionUrl, fromProperties(connectionProperties));
} catch (SQLException e) {
throw new PrestoException(JDBC_ERROR, e);
}
}
use of com.facebook.presto.spi.ColumnMetadata in project presto by prestodb.
the class TestJdbcMetadata method getTableMetadata.
@Test
public void getTableMetadata() {
// known table
ConnectorTableMetadata tableMetadata = metadata.getTableMetadata(SESSION, tableHandle);
assertEquals(tableMetadata.getTable(), new SchemaTableName("example", "numbers"));
assertEquals(tableMetadata.getColumns(), ImmutableList.of(new ColumnMetadata("text", VARCHAR), new ColumnMetadata("text_short", createVarcharType(32)), new ColumnMetadata("value", BIGINT)));
// escaping name patterns
JdbcTableHandle specialTableHandle = metadata.getTableHandle(SESSION, new SchemaTableName("exa_ple", "num_ers"));
ConnectorTableMetadata specialTableMetadata = metadata.getTableMetadata(SESSION, specialTableHandle);
assertEquals(specialTableMetadata.getTable(), new SchemaTableName("exa_ple", "num_ers"));
assertEquals(specialTableMetadata.getColumns(), ImmutableList.of(new ColumnMetadata("te_t", VARCHAR), new ColumnMetadata("va%ue", BIGINT)));
// unknown tables should produce null
unknownTableMetadata(new JdbcTableHandle(CONNECTOR_ID, new SchemaTableName("u", "numbers"), null, "unknown", "unknown"));
unknownTableMetadata(new JdbcTableHandle(CONNECTOR_ID, new SchemaTableName("example", "numbers"), null, "example", "unknown"));
unknownTableMetadata(new JdbcTableHandle(CONNECTOR_ID, new SchemaTableName("example", "numbers"), null, "unknown", "numbers"));
}
Aggregations