Skip to content

Commit

Permalink
Move schemaFromHandles method to IcebergUtil
Browse files Browse the repository at this point in the history
  • Loading branch information
electrum committed Jul 21, 2022
1 parent 4d15cc6 commit 2b63607
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@
import static io.trino.plugin.iceberg.IcebergUtil.getSnapshotIdAsOfTime;
import static io.trino.plugin.iceberg.IcebergUtil.getTableComment;
import static io.trino.plugin.iceberg.IcebergUtil.newCreateTableTransaction;
import static io.trino.plugin.iceberg.IcebergUtil.toIcebergSchema;
import static io.trino.plugin.iceberg.IcebergUtil.schemaFromMetadata;
import static io.trino.plugin.iceberg.PartitionFields.parsePartitionFields;
import static io.trino.plugin.iceberg.PartitionFields.toPartitionFields;
import static io.trino.plugin.iceberg.TableType.DATA;
Expand Down Expand Up @@ -627,7 +627,7 @@ public void setViewComment(ConnectorSession session, SchemaTableName viewName, O
@Override
public Optional<ConnectorTableLayout> getNewTableLayout(ConnectorSession session, ConnectorTableMetadata tableMetadata)
{
Schema schema = toIcebergSchema(tableMetadata.getColumns());
Schema schema = schemaFromMetadata(tableMetadata.getColumns());
PartitionSpec partitionSpec = parsePartitionFields(schema, getPartitioning(tableMetadata.getProperties()));
return getWriteLayout(schema, partitionSpec, false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,14 @@
import io.trino.spi.type.TypeManager;
import io.trino.spi.type.TypeOperators;
import org.apache.iceberg.Schema;
import org.apache.iceberg.types.Types;

import javax.inject.Inject;

import java.util.List;
import java.util.function.ToIntFunction;

import static com.google.common.collect.ImmutableList.toImmutableList;
import static io.trino.plugin.iceberg.IcebergUtil.schemaFromHandles;
import static io.trino.plugin.iceberg.PartitionFields.parsePartitionFields;
import static io.trino.plugin.iceberg.TypeConverter.toIcebergType;
import static io.trino.spi.connector.ConnectorBucketNodeMap.createBucketNodeMap;
import static java.util.Objects.requireNonNull;

Expand Down Expand Up @@ -78,23 +76,11 @@ public BucketFunction getBucketFunction(
int bucketCount)
{
IcebergPartitioningHandle handle = (IcebergPartitioningHandle) partitioningHandle;
Schema schema = toIcebergSchema(handle.getPartitioningColumns());
Schema schema = schemaFromHandles(handle.getPartitioningColumns());
return new IcebergBucketFunction(
typeOperators,
parsePartitionFields(schema, handle.getPartitioning()),
handle.getPartitioningColumns(),
bucketCount);
}

private static Schema toIcebergSchema(List<IcebergColumnHandle> columns)
{
List<Types.NestedField> icebergColumns = columns.stream()
.map(column -> {
org.apache.iceberg.types.Type type = toIcebergType(column.getType());
return Types.NestedField.of(column.getId(), true, column.getName(), type);
})
.collect(toImmutableList());
org.apache.iceberg.types.Type icebergSchema = Types.StructType.of(icebergColumns);
return new Schema(icebergSchema.asStructType().fields());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,14 @@ public static IcebergColumnHandle getColumnHandle(NestedField column, TypeManage
Optional.ofNullable(column.doc()));
}

public static Schema schemaFromHandles(List<IcebergColumnHandle> columns)
{
List<NestedField> icebergColumns = columns.stream()
.map(column -> NestedField.optional(column.getId(), column.getName(), toIcebergType(column.getType())))
.collect(toImmutableList());
return new Schema(StructType.of(icebergColumns).asStructType().fields());
}

public static Map<PartitionField, Integer> getIdentityPartitions(PartitionSpec partitionSpec)
{
// TODO: expose transform information in Iceberg library
Expand Down Expand Up @@ -556,7 +564,7 @@ public static LocationProvider getLocationProvider(SchemaTableName schemaTableNa
return locationsFor(tableLocation, storageProperties);
}

public static Schema toIcebergSchema(List<ColumnMetadata> columns)
public static Schema schemaFromMetadata(List<ColumnMetadata> columns)
{
List<NestedField> icebergColumns = new ArrayList<>();
for (ColumnMetadata column : columns) {
Expand All @@ -576,7 +584,7 @@ public static Schema toIcebergSchema(List<ColumnMetadata> columns)
public static Transaction newCreateTableTransaction(TrinoCatalog catalog, ConnectorTableMetadata tableMetadata, ConnectorSession session)
{
SchemaTableName schemaTableName = tableMetadata.getTable();
Schema schema = toIcebergSchema(tableMetadata.getColumns());
Schema schema = schemaFromMetadata(tableMetadata.getColumns());
PartitionSpec partitionSpec = parsePartitionFields(schema, getPartitioning(tableMetadata.getProperties()));
String targetPath = getTableLocation(tableMetadata.getProperties())
.orElseGet(() -> catalog.defaultTableLocation(session, schemaTableName));
Expand Down

0 comments on commit 2b63607

Please sign in to comment.