Skip to content

Commit a1f326f

Browse files
committed
Core,Open-API: Don't expose the last-column-id
I've added this to the spec a while ago: apache#7445 But I think this was a mistake, and we should not expose this to the public APIs, as it is much better to track this internally. I noticed this while reviewing apache/iceberg-rust#587 Removing this as part of the APIs in Java, and the Open-API update makes it much more resilient, and don't require the clients to compute this value
1 parent 82a2362 commit a1f326f

File tree

7 files changed

+41
-17
lines changed

7 files changed

+41
-17
lines changed

core/src/main/java/org/apache/iceberg/MetadataUpdate.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,16 @@ class AddSchema implements MetadataUpdate {
8686
private final Schema schema;
8787
private final int lastColumnId;
8888

89+
public AddSchema(Schema schema) {
90+
this(schema, schema.highestFieldId());
91+
}
92+
93+
/**
94+
* Set the schema
95+
*
96+
* @deprecated since 1.8.0, will be removed 1.9.0 or 2.0.0, use AddSchema(schema).
97+
*/
98+
@Deprecated
8999
public AddSchema(Schema schema, int lastColumnId) {
90100
this.schema = schema;
91101
this.lastColumnId = lastColumnId;

core/src/main/java/org/apache/iceberg/MetadataUpdateParser.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,11 @@
3030
import org.apache.iceberg.relocated.com.google.common.collect.Iterables;
3131
import org.apache.iceberg.util.JsonUtil;
3232
import org.apache.iceberg.view.ViewVersionParser;
33+
import org.slf4j.Logger;
34+
import org.slf4j.LoggerFactory;
3335

3436
public class MetadataUpdateParser {
37+
private static final Logger LOG = LoggerFactory.getLogger(MetadataUpdateParser.class);
3538

3639
private MetadataUpdateParser() {}
3740

@@ -462,6 +465,7 @@ private static MetadataUpdate readAddSchema(JsonNode node) {
462465
Schema schema = SchemaParser.fromJson(schemaNode);
463466
int lastColumnId;
464467
if (node.has(LAST_COLUMN_ID)) {
468+
LOG.warn("Field last-column-id of MetadataUpdate.AddSchema is since 1.8.0, will be removed 1.9.0 or 2.0.0");
465469
lastColumnId = JsonUtil.getInt(LAST_COLUMN_ID, node);
466470
} else {
467471
lastColumnId = schema.highestFieldId();

core/src/main/java/org/apache/iceberg/SchemaUpdate.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ public Schema apply() {
444444

445445
@Override
446446
public void commit() {
447-
TableMetadata update = applyChangesToMetadata(base.updateSchema(apply(), lastColumnId));
447+
TableMetadata update = applyChangesToMetadata(base.updateSchema(apply()));
448448
ops.commit(base, update);
449449
}
450450

core/src/main/java/org/apache/iceberg/TableMetadata.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -562,10 +562,23 @@ public TableMetadata withUUID() {
562562
return new Builder(this).assignUUID().build();
563563
}
564564

565+
/**
566+
* Updates the schema
567+
*
568+
* @deprecated since 1.8.0, will be removed 1.9.0 or 2.0.0, use updateSchema(schema).
569+
*/
570+
@Deprecated
565571
public TableMetadata updateSchema(Schema newSchema, int newLastColumnId) {
566572
return new Builder(this).setCurrentSchema(newSchema, newLastColumnId).build();
567573
}
568574

575+
/**
576+
* Updates the schema
577+
*/
578+
public TableMetadata updateSchema(Schema newSchema) {
579+
return new Builder(this).setCurrentSchema(newSchema, Math.max(this.lastColumnId, newSchema.highestFieldId())).build();
580+
}
581+
569582
// The caller is responsible to pass a newPartitionSpec with correct partition field IDs
570583
public TableMetadata updatePartitionSpec(PartitionSpec newPartitionSpec) {
571584
return new Builder(this).setDefaultPartitionSpec(newPartitionSpec).build();
@@ -1081,8 +1094,18 @@ public Builder setCurrentSchema(int schemaId) {
10811094
return this;
10821095
}
10831096

1097+
public Builder addSchema(Schema schema) {
1098+
addSchemaInternal(schema, schema.highestFieldId());
1099+
return this;
1100+
}
1101+
1102+
/**
1103+
* Add a new schema.
1104+
*
1105+
* @deprecated since 1.8.0, will be removed 1.9.0 or 2.0.0, use AddSchema(schema).
1106+
*/
1107+
@Deprecated
10841108
public Builder addSchema(Schema schema, int newLastColumnId) {
1085-
// TODO: remove requirement for newLastColumnId
10861109
addSchemaInternal(schema, newLastColumnId);
10871110
return this;
10881111
}
@@ -1525,7 +1548,7 @@ && changes(MetadataUpdate.AddSchema.class)
15251548
return newSchemaId;
15261549
}
15271550

1528-
this.lastColumnId = newLastColumnId;
1551+
this.lastColumnId = Math.max(lastColumnId, newLastColumnId);
15291552

15301553
Schema newSchema;
15311554
if (newSchemaId != schema.schemaId()) {

core/src/main/java/org/apache/iceberg/view/ViewMetadata.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -372,20 +372,15 @@ private int addSchemaInternal(Schema schema) {
372372
newSchema = schema;
373373
}
374374

375-
int highestFieldId = Math.max(highestFieldId(), newSchema.highestFieldId());
376375
schemas.add(newSchema);
377376
schemasById.put(newSchema.schemaId(), newSchema);
378-
changes.add(new MetadataUpdate.AddSchema(newSchema, highestFieldId));
377+
changes.add(new MetadataUpdate.AddSchema(newSchema));
379378

380379
this.lastAddedSchemaId = newSchemaId;
381380

382381
return newSchemaId;
383382
}
384383

385-
private int highestFieldId() {
386-
return schemas.stream().map(Schema::highestFieldId).max(Integer::compareTo).orElse(0);
387-
}
388-
389384
private int reuseOrCreateNewSchemaId(Schema newSchema) {
390385
// if the schema already exists, use its id; otherwise use the highest id + 1
391386
int newSchemaId = INITIAL_SCHEMA_ID;

open-api/rest-catalog-open-api.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1149,11 +1149,6 @@ class ViewMetadata(BaseModel):
11491149
class AddSchemaUpdate(BaseUpdate):
11501150
action: Literal['add-schema']
11511151
schema_: Schema = Field(..., alias='schema')
1152-
last_column_id: Optional[int] = Field(
1153-
None,
1154-
alias='last-column-id',
1155-
description='The highest assigned column ID for the table. This is used to ensure columns are always assigned an unused ID when evolving schemas. When omitted, it will be computed on the server side.',
1156-
)
11571152

11581153

11591154
class TableUpdate(BaseModel):

open-api/rest-catalog-open-api.yaml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2690,9 +2690,6 @@ components:
26902690
enum: ["add-schema"]
26912691
schema:
26922692
$ref: '#/components/schemas/Schema'
2693-
last-column-id:
2694-
type: integer
2695-
description: The highest assigned column ID for the table. This is used to ensure columns are always assigned an unused ID when evolving schemas. When omitted, it will be computed on the server side.
26962693

26972694
SetCurrentSchemaUpdate:
26982695
allOf:

0 commit comments

Comments
 (0)