Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions api/src/main/java/org/apache/iceberg/Schema.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ public Schema(int schemaId, List<NestedField> columns, Set<Integer> identifierFi
this(schemaId, columns, null, identifierFieldIds);
}

private Schema(int schemaId, List<NestedField> columns, Map<String, Integer> aliases,
Set<Integer> identifierFieldIds) {
public Schema(int schemaId, List<NestedField> columns, Map<String, Integer> aliases,
Set<Integer> identifierFieldIds) {
this.schemaId = schemaId;
this.struct = StructType.of(columns);
this.aliasToId = aliases != null ? ImmutableBiMap.copyOf(aliases) : null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -467,10 +467,6 @@ private static void validateFlinkTable(CatalogBaseTable table) {
if (!schema.getWatermarkSpecs().isEmpty()) {
throw new UnsupportedOperationException("Creating table with watermark specs is not supported yet.");
}

if (schema.getPrimaryKey().isPresent()) {
throw new UnsupportedOperationException("Creating table with primary key is not supported yet.");
}
}

private static PartitionSpec toPartitionSpec(List<String> partitionKeys, Schema icebergSchema) {
Expand Down Expand Up @@ -536,7 +532,7 @@ private static void commitChanges(Table table, String setLocation, String setSna
}

static CatalogTable toCatalogTable(Table table) {
TableSchema schema = FlinkSchemaUtil.toSchema(FlinkSchemaUtil.convert(table.schema()));
TableSchema schema = FlinkSchemaUtil.toSchema(table.schema());
List<String> partitionKeys = toPartitionKeys(table.spec(), table.schema());

// NOTE: We can not create a IcebergCatalogTable extends CatalogTable, because Flink optimizer may use
Expand Down
55 changes: 53 additions & 2 deletions flink/src/main/java/org/apache/iceberg/flink/FlinkSchemaUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,16 @@

package org.apache.iceberg.flink;

import java.util.List;
import java.util.Set;
import org.apache.flink.table.api.TableSchema;
import org.apache.flink.table.types.logical.LogicalType;
import org.apache.flink.table.types.logical.RowType;
import org.apache.flink.table.types.utils.TypeConversions;
import org.apache.iceberg.Schema;
import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
import org.apache.iceberg.relocated.com.google.common.collect.Lists;
import org.apache.iceberg.relocated.com.google.common.collect.Sets;
import org.apache.iceberg.types.Type;
import org.apache.iceberg.types.TypeUtil;
import org.apache.iceberg.types.Types;
Expand Down Expand Up @@ -61,7 +65,23 @@ public static Schema convert(TableSchema schema) {
RowType root = (RowType) schemaType;
Type converted = root.accept(new FlinkTypeToType(root));

return new Schema(converted.asStructType().fields());
Schema iSchema = new Schema(converted.asStructType().fields());
return freshIdentifierFieldIds(iSchema, schema);
}

private static Schema freshIdentifierFieldIds(Schema iSchema, TableSchema schema) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of having this method, if we expose a TypeUtil.freshIdentifierFieldIds(Schema iSchema, Schema base), can we do a conversion of Flink to Iceberg schema and then call that method?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we can reuse the TypeUtil.refreshIdentifierFields(Types.StructType freshSchema, Schema baseSchema) because in this method we've had an existing identifier field id list inside the baseSchema. While for this case, the identifier fields are actually came from flink's TableSchema, converting the flink's TableSchema with primary keys to the iceberg table schema with identifier field id list is exactly the thing we are trying to accomplish in this method.

// Locate the identifier field id list.
Set<Integer> identifierFieldIds = Sets.newHashSet();
if (schema.getPrimaryKey().isPresent()) {
for (String column : schema.getPrimaryKey().get().getColumns()) {
Types.NestedField field = iSchema.findField(column);
Preconditions.checkNotNull(field,
"Cannot find field ID for the primary key column %s in schema %s", column, iSchema);
identifierFieldIds.add(field.fieldId());
}
}

return new Schema(iSchema.schemaId(), iSchema.asStruct().fields(), identifierFieldIds);
}

/**
Expand All @@ -83,7 +103,8 @@ public static Schema convert(Schema baseSchema, TableSchema flinkSchema) {
// reassign ids to match the base schema
Schema schema = TypeUtil.reassignIds(new Schema(struct.fields()), baseSchema);
// fix types that can't be represented in Flink (UUID)
return FlinkFixupTypes.fixup(schema, baseSchema);
Schema fixedSchema = FlinkFixupTypes.fixup(schema, baseSchema);
return freshIdentifierFieldIds(fixedSchema, flinkSchema);
}

/**
Expand Down Expand Up @@ -121,4 +142,34 @@ public static TableSchema toSchema(RowType rowType) {
}
return builder.build();
}

/**
* Convert a {@link Schema} to a {@link TableSchema}.
*
* @param schema iceberg schema to convert.
* @return Flink TableSchema.
*/
public static TableSchema toSchema(Schema schema) {
TableSchema.Builder builder = TableSchema.builder();

// Add columns.
for (RowType.RowField field : convert(schema).getFields()) {
builder.field(field.getName(), TypeConversions.fromLogicalToDataType(field.getType()));
}

// Add primary key.
Set<Integer> identifierFieldIds = schema.identifierFieldIds();
if (!identifierFieldIds.isEmpty()) {
List<String> columns = Lists.newArrayListWithExpectedSize(identifierFieldIds.size());
for (Integer identifierFieldId : identifierFieldIds) {
String columnName = schema.findColumnName(identifierFieldId);
Preconditions.checkNotNull(columnName, "Cannot find field with id %s in schema %s", identifierFieldId, schema);

columns.add(columnName);
}
builder.primaryKey(columns.toArray(new String[0]));
}

return builder.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@

package org.apache.iceberg.flink;

import java.util.List;
import java.util.Map;
import org.apache.flink.table.api.TableSchema;
import org.apache.flink.table.api.constraints.UniqueConstraint;
import org.apache.flink.table.connector.ChangelogMode;
import org.apache.flink.table.connector.sink.DataStreamSinkProvider;
import org.apache.flink.table.connector.sink.DynamicTableSink;
Expand All @@ -29,6 +31,7 @@
import org.apache.flink.types.RowKind;
import org.apache.flink.util.Preconditions;
import org.apache.iceberg.flink.sink.FlinkSink;
import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList;

public class IcebergTableSink implements DynamicTableSink, SupportsPartitioning, SupportsOverwrite {
private final TableLoader tableLoader;
Expand All @@ -52,9 +55,14 @@ public SinkRuntimeProvider getSinkRuntimeProvider(Context context) {
Preconditions.checkState(!overwrite || context.isBounded(),
"Unbounded data stream doesn't support overwrite operation.");

List<String> equalityColumns = tableSchema.getPrimaryKey()
.map(UniqueConstraint::getColumns)
.orElseGet(ImmutableList::of);

return (DataStreamSinkProvider) dataStream -> FlinkSink.forRowData(dataStream)
.tableLoader(tableLoader)
.tableSchema(tableSchema)
.equalityFieldColumns(equalityColumns)
.overwrite(overwrite)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,17 @@ public static StructLikeSet expectedRowSet(Table table, Record... records) {
}

public static StructLikeSet actualRowSet(Table table, String... columns) throws IOException {
return actualRowSet(table, null, columns);
}

public static StructLikeSet actualRowSet(Table table, Long snapshotId, String... columns) throws IOException {
table.refresh();
StructLikeSet set = StructLikeSet.create(table.schema().asStruct());
try (CloseableIterable<Record> reader = IcebergGenerics.read(table).select(columns).build()) {
try (CloseableIterable<Record> reader = IcebergGenerics
.read(table)
.useSnapshot(snapshotId == null ? table.currentSnapshot().snapshotId() : snapshotId)
.select(columns)
.build()) {
reader.forEach(set::add);
}
return set;
Expand Down
Loading