Skip to content

Commit

Permalink
Core: Add serialization for AddPartitionSpec, AddSortOrder (#4668)
Browse files Browse the repository at this point in the history
  • Loading branch information
kbendick authored May 3, 2022
1 parent c38f7b5 commit 62f3ca3
Show file tree
Hide file tree
Showing 2 changed files with 262 additions and 71 deletions.
103 changes: 68 additions & 35 deletions core/src/main/java/org/apache/iceberg/MetadataUpdateParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,21 @@ private MetadataUpdateParser() {

private static final String ACTION = "action";

// action types
private static final String ASSIGN_UUID = "assign-uuid";
private static final String UPGRADE_FORMAT_VERSION = "upgrade-format-version";
private static final String ADD_SCHEMA = "add-schema";
private static final String SET_CURRENT_SCHEMA = "set-current-schema";
private static final String ADD_PARTITION_SPEC = "add-spec";
private static final String SET_DEFAULT_PARTITION_SPEC = "set-default-spec";
private static final String ADD_SORT_ORDER = "add-sort-order";
private static final String SET_DEFAULT_SORT_ORDER = "set-default-sort-order";
private static final String ADD_SNAPSHOT = "add-snapshot";
private static final String REMOVE_SNAPSHOTS = "remove-snapshots";
private static final String SET_SNAPSHOT_REF = "set-snapshot-ref";
private static final String SET_PROPERTIES = "set-properties";
private static final String REMOVE_PROPERTIES = "remove-properties";
private static final String SET_LOCATION = "set-location";
// action types - visible for testing
static final String ASSIGN_UUID = "assign-uuid";
static final String UPGRADE_FORMAT_VERSION = "upgrade-format-version";
static final String ADD_SCHEMA = "add-schema";
static final String SET_CURRENT_SCHEMA = "set-current-schema";
static final String ADD_PARTITION_SPEC = "add-spec";
static final String SET_DEFAULT_PARTITION_SPEC = "set-default-spec";
static final String ADD_SORT_ORDER = "add-sort-order";
static final String SET_DEFAULT_SORT_ORDER = "set-default-sort-order";
static final String ADD_SNAPSHOT = "add-snapshot";
static final String REMOVE_SNAPSHOTS = "remove-snapshots";
static final String SET_SNAPSHOT_REF = "set-snapshot-ref";
static final String SET_PROPERTIES = "set-properties";
static final String REMOVE_PROPERTIES = "remove-properties";
static final String SET_LOCATION = "set-location";

// UpgradeFormatVersion
private static final String FORMAT_VERSION = "format-version";
Expand All @@ -69,6 +69,9 @@ private MetadataUpdateParser() {
// SetDefaultPartitionSpec
private static final String SPEC_ID = "spec-id";

// AddSortOrder
private static final String SORT_ORDER = "sort-order";

private static final Map<Class<? extends MetadataUpdate>, String> ACTIONS = ImmutableMap
.<Class<? extends MetadataUpdate>, String>builder()
.put(MetadataUpdate.AssignUUID.class, ASSIGN_UUID)
Expand Down Expand Up @@ -116,19 +119,23 @@ public static void toJson(MetadataUpdate metadataUpdate, JsonGenerator generator
case ASSIGN_UUID:
throw new UnsupportedOperationException("Not Implemented: MetadataUpdate#toJson for AssignUUID");
case UPGRADE_FORMAT_VERSION:
writeAsUpgradeFormatVersion((MetadataUpdate.UpgradeFormatVersion) metadataUpdate, generator);
writeUpgradeFormatVersion((MetadataUpdate.UpgradeFormatVersion) metadataUpdate, generator);
break;
case ADD_SCHEMA:
writeAsAddSchema((MetadataUpdate.AddSchema) metadataUpdate, generator);
writeAddSchema((MetadataUpdate.AddSchema) metadataUpdate, generator);
break;
case SET_CURRENT_SCHEMA:
writeAsSetCurrentSchema((MetadataUpdate.SetCurrentSchema) metadataUpdate, generator);
writeSetCurrentSchema((MetadataUpdate.SetCurrentSchema) metadataUpdate, generator);
break;
case ADD_PARTITION_SPEC:
writeAddPartitionSpec((MetadataUpdate.AddPartitionSpec) metadataUpdate, generator);
break;
case SET_DEFAULT_PARTITION_SPEC:
writeAsSetDefaultPartitionSpec((MetadataUpdate.SetDefaultPartitionSpec) metadataUpdate, generator);
writeSetDefaultPartitionSpec((MetadataUpdate.SetDefaultPartitionSpec) metadataUpdate, generator);
break;
case ADD_PARTITION_SPEC:
case ADD_SORT_ORDER:
writeAddSortOrder((MetadataUpdate.AddSortOrder) metadataUpdate, generator);
break;
case SET_DEFAULT_SORT_ORDER:
case ADD_SNAPSHOT:
case REMOVE_SNAPSHOTS:
Expand Down Expand Up @@ -169,15 +176,17 @@ public static MetadataUpdate fromJson(JsonNode jsonNode) {
case ASSIGN_UUID:
throw new UnsupportedOperationException("Not implemented: AssignUUID");
case UPGRADE_FORMAT_VERSION:
return readAsUpgradeFormatVersion(jsonNode);
return readUpgradeFormatVersion(jsonNode);
case ADD_SCHEMA:
return readAsAddSchema(jsonNode);
return readAddSchema(jsonNode);
case SET_CURRENT_SCHEMA:
return readAsSetCurrentSchema(jsonNode);
case SET_DEFAULT_PARTITION_SPEC:
return readAsSetDefaultPartitionSpec(jsonNode);
return readSetCurrentSchema(jsonNode);
case ADD_PARTITION_SPEC:
return readAddPartitionSpec(jsonNode);
case SET_DEFAULT_PARTITION_SPEC:
return readSetDefaultPartitionSpec(jsonNode);
case ADD_SORT_ORDER:
return readAddSortOrder(jsonNode);
case SET_DEFAULT_SORT_ORDER:
case ADD_SNAPSHOT:
case REMOVE_SNAPSHOTS:
Expand All @@ -192,51 +201,75 @@ public static MetadataUpdate fromJson(JsonNode jsonNode) {
}
}

private static void writeAsUpgradeFormatVersion(MetadataUpdate.UpgradeFormatVersion update, JsonGenerator gen)
private static void writeUpgradeFormatVersion(MetadataUpdate.UpgradeFormatVersion update, JsonGenerator gen)
throws IOException {
gen.writeNumberField(FORMAT_VERSION, update.formatVersion());
}

private static void writeAsAddSchema(MetadataUpdate.AddSchema update, JsonGenerator gen) throws IOException {
private static void writeAddSchema(MetadataUpdate.AddSchema update, JsonGenerator gen) throws IOException {
gen.writeFieldName(SCHEMA);
SchemaParser.toJson(update.schema(), gen);
gen.writeNumberField(LAST_COLUMN_ID, update.lastColumnId());
}

private static void writeAsSetCurrentSchema(MetadataUpdate.SetCurrentSchema update, JsonGenerator gen)
private static void writeSetCurrentSchema(MetadataUpdate.SetCurrentSchema update, JsonGenerator gen)
throws IOException {
gen.writeNumberField(SCHEMA_ID, update.schemaId());
}

private static void writeAsSetDefaultPartitionSpec(MetadataUpdate.SetDefaultPartitionSpec update, JsonGenerator gen)
private static void writeAddPartitionSpec(MetadataUpdate.AddPartitionSpec update, JsonGenerator gen)
throws IOException {
gen.writeFieldName(SPEC);
PartitionSpecParser.toJson(update.spec(), gen);
}

private static void writeSetDefaultPartitionSpec(MetadataUpdate.SetDefaultPartitionSpec update, JsonGenerator gen)
throws IOException {
gen.writeNumberField(SPEC_ID, update.specId());
}

private static MetadataUpdate readAsUpgradeFormatVersion(JsonNode node) {
private static void writeAddSortOrder(MetadataUpdate.AddSortOrder update, JsonGenerator gen)
throws IOException {
gen.writeFieldName(SORT_ORDER);
SortOrderParser.toJson(update.sortOrder(), gen);
}

private static MetadataUpdate readUpgradeFormatVersion(JsonNode node) {
int formatVersion = JsonUtil.getInt(FORMAT_VERSION, node);
return new MetadataUpdate.UpgradeFormatVersion(formatVersion);
}

private static MetadataUpdate readAsAddSchema(JsonNode node) {
private static MetadataUpdate readAddSchema(JsonNode node) {
Preconditions.checkArgument(node.hasNonNull(SCHEMA),
"Cannot parse missing field: schema");
JsonNode schemaNode = node.get(SCHEMA);
Preconditions.checkArgument(schemaNode.isObject(),
"Invalid type for schema field. Expected object");
Schema schema = SchemaParser.fromJson(schemaNode);
int lastColumnId = JsonUtil.getInt(LAST_COLUMN_ID, node);
return new MetadataUpdate.AddSchema(schema, lastColumnId);
}

private static MetadataUpdate readAsSetCurrentSchema(JsonNode node) {
private static MetadataUpdate readSetCurrentSchema(JsonNode node) {
int schemaId = JsonUtil.getInt(SCHEMA_ID, node);
return new MetadataUpdate.SetCurrentSchema(schemaId);
}

private static MetadataUpdate readAsSetDefaultPartitionSpec(JsonNode node) {
private static MetadataUpdate readAddPartitionSpec(JsonNode node) {
Preconditions.checkArgument(node.hasNonNull(SPEC), "Missing required field: spec");
JsonNode specNode = node.get(SPEC);
UnboundPartitionSpec spec = PartitionSpecParser.fromJson(specNode);
return new MetadataUpdate.AddPartitionSpec(spec);
}

private static MetadataUpdate readSetDefaultPartitionSpec(JsonNode node) {
int specId = JsonUtil.getInt(SPEC_ID, node);
return new MetadataUpdate.SetDefaultPartitionSpec(specId);
}

private static MetadataUpdate readAddSortOrder(JsonNode node) {
Preconditions.checkArgument(node.hasNonNull(SORT_ORDER), "Cannot parse missing field: sort-order");
JsonNode sortOrderNode = node.get(SORT_ORDER);
UnboundSortOrder sortOrder = SortOrderParser.fromJson(sortOrderNode);
return new MetadataUpdate.AddSortOrder(sortOrder);
}
}

Loading

0 comments on commit 62f3ca3

Please sign in to comment.