pvary commented on code in PR #13072:
URL: https://github.com/apache/iceberg/pull/13072#discussion_r2140552840


##########
flink/v1.20/flink/src/main/java/org/apache/iceberg/flink/FlinkSchemaUtil.java:
##########
@@ -231,4 +290,90 @@ public static TableSchema toSchema(Schema schema) {
 
     return builder.build();
   }
+
+  /**
+   * Convert a {@link Schema} to a {@link ResolvedSchema}.
+   *
+   * @param schema iceberg schema to convert.
+   * @return Flink ResolvedSchema.
+   */
+  public static ResolvedSchema toResolvedSchema(Schema schema) {
+    RowType rowType = convert(schema);
+    List<Column> columns = 
Lists.newArrayListWithExpectedSize(rowType.getFieldCount());
+
+    // Add columns.
+    for (RowType.RowField field : rowType.getFields()) {
+      columns.add(
+          Column.physical(field.getName(), 
TypeConversions.fromLogicalToDataType(field.getType())));
+    }
+
+    // Add primary key.
+    Set<Integer> identifierFieldIds = schema.identifierFieldIds();
+    UniqueConstraint uniqueConstraint = null;
+    if (!identifierFieldIds.isEmpty()) {
+      List<String> primaryKeyColumns =
+          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);
+
+        primaryKeyColumns.add(columnName);
+      }
+
+      uniqueConstraint =
+          UniqueConstraint.primaryKey(UUID.randomUUID().toString(), 
primaryKeyColumns);
+
+      validatePrimaryKey(uniqueConstraint, columns);
+    }
+
+    return new ResolvedSchema(columns, Collections.emptyList(), 
uniqueConstraint);
+  }
+
+  /**
+   * Copied from
+   * 
org.apache.flink.table.catalog.DefaultSchemaResolver#validatePrimaryKey(org.apache.flink.table.catalog.UniqueConstraint,
+   * java.util.List)
+   */

Review Comment:
   We need to update the `/LICENSE` to mentioned the copied code



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org
For additional commands, e-mail: issues-h...@iceberg.apache.org

Reply via email to