You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@iceberg.apache.org by GitBox <gi...@apache.org> on 2022/04/21 02:54:05 UTC

[GitHub] [iceberg] kbendick opened a new pull request, #4601: [CORE] - Add MetadataUpdateParser and corresponding serlializer / deserializer for UpgradeFormatVersion

kbendick opened a new pull request, #4601:
URL: https://github.com/apache/iceberg/pull/4601

   We need to be able to serialize and deserialize instances of `MetadataUpdate`.
   
   Because `MetadataUpdate` is an interface that has multiple implementations, I've added a serializer / deserializer that handles `MetadataUpdate` and then reads or write the correct value based on the `action` field, as defined in the open-api spec.
   
   I've only implemented  and tested`MetadataUpdate#UpgradeFormatVersion`, and have laid the foundation for the remaining classes which I will handle in a follow up PR.


-- 
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-unsubscribe@iceberg.apache.org

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


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


[GitHub] [iceberg] kbendick commented on a diff in pull request #4601: [CORE] - Add MetadataUpdateParser and Jackson SerDe with implementation covering MetadataUpdate#UpgradeFormatVersion

Posted by GitBox <gi...@apache.org>.
kbendick commented on code in PR #4601:
URL: https://github.com/apache/iceberg/pull/4601#discussion_r854735104


##########
core/src/main/java/org/apache/iceberg/MetadataUpdateParser.java:
##########
@@ -0,0 +1,163 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.iceberg;
+
+import com.fasterxml.jackson.core.JsonGenerator;
+import com.fasterxml.jackson.databind.JsonNode;
+import java.io.IOException;
+import java.io.StringWriter;
+import java.io.UncheckedIOException;
+import java.io.UnsupportedEncodingException;
+import java.util.Locale;
+import java.util.Map;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap;
+import org.apache.iceberg.util.JsonUtil;
+
+public class MetadataUpdateParser {
+
+  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_SNAPSHOT = "remove-snapshot";
+  private static final String REMOVE_SNAPSHOT_REF = "remove-snapshot-ref";
+  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";
+
+  // UpgradeFormatVersion
+  private static final String FORMAT_VERSION = "format-version";
+
+  private static final Map<Class<? extends MetadataUpdate>, String> ACTIONS = ImmutableMap
+      .<Class<? extends MetadataUpdate>, String>builder()
+      .put(MetadataUpdate.AssignUUID.class, ASSIGN_UUID)
+      .put(MetadataUpdate.UpgradeFormatVersion.class, UPGRADE_FORMAT_VERSION)
+      .put(MetadataUpdate.AddSchema.class, ADD_SCHEMA)
+      .put(MetadataUpdate.SetCurrentSchema.class, SET_CURRENT_SCHEMA)
+      .put(MetadataUpdate.AddPartitionSpec.class, ADD_PARTITION_SPEC)
+      .put(MetadataUpdate.SetDefaultPartitionSpec.class, SET_DEFAULT_PARTITION_SPEC)
+      .put(MetadataUpdate.AddSortOrder.class, ADD_SORT_ORDER)
+      .put(MetadataUpdate.SetDefaultSortOrder.class, SET_DEFAULT_SORT_ORDER)
+      .put(MetadataUpdate.AddSnapshot.class, ADD_SNAPSHOT)
+      .put(MetadataUpdate.RemoveSnapshot.class, REMOVE_SNAPSHOT)
+      .put(MetadataUpdate.RemoveSnapshotRef.class, REMOVE_SNAPSHOT_REF)
+      .put(MetadataUpdate.SetSnapshotRef.class, SET_SNAPSHOT_REF)
+      .put(MetadataUpdate.SetProperties.class, SET_PROPERTIES)
+      .put(MetadataUpdate.RemoveProperties.class, REMOVE_PROPERTIES)
+      .put(MetadataUpdate.SetLocation.class, SET_LOCATION)
+      .build();
+
+  public static String toJson(MetadataUpdate metadataUpdate) {
+    return toJson(metadataUpdate, false);
+  }
+
+  public static String toJson(MetadataUpdate metadataUpdate, boolean pretty) {
+    try {
+      StringWriter writer = new StringWriter();
+      JsonGenerator generator = JsonUtil.factory().createGenerator(writer);
+      if (pretty) {
+        generator.useDefaultPrettyPrinter();
+      }
+      toJson(metadataUpdate, generator);
+      generator.flush();
+      return writer.toString();
+    } catch (IOException e) {
+      throw new UncheckedIOException(String.format("Failed to write metadata update json for: %s", metadataUpdate), e);
+    }
+  }
+
+  public static void toJson(MetadataUpdate metadataUpdate, JsonGenerator generator) throws IOException {
+    String updateAction = ACTIONS.get(metadataUpdate.getClass());
+
+    // Add shared `action` field used to differentiate between implementations of MetadataUpdate
+    generator.writeStartObject();
+    generator.writeStringField(ACTION, updateAction);
+
+    // Write subclass specific fields
+    switch (updateAction) {
+      case ASSIGN_UUID:
+        throw new UnsupportedOperationException("Not Implemented: MetadataUpdate#toJson for AssignUUID");
+      case UPGRADE_FORMAT_VERSION:
+        writeUpgradeFormatVersion(metadataUpdate, generator);
+        break;
+      default:
+        throw new IllegalArgumentException(
+            String.format("Cannot convert metadata update to json. Unrecognized action: %s", updateAction));
+    }
+
+    generator.writeEndObject();
+  }
+
+  /**
+   * Read MetadataUpdate from a JSON string.
+   *
+   * @param json a JSON string of a MetadataUpdate
+   * @return a MetadataUpdate object
+   */
+  public static MetadataUpdate fromJson(String json) {
+    try {
+      return fromJson(JsonUtil.mapper().readValue(json, JsonNode.class));
+    } catch (IOException e) {
+      throw new UncheckedIOException("Failed to read JSON string: " + json, e);
+    }
+  }
+
+  public static MetadataUpdate fromJson(JsonNode jsonNode) {
+    Preconditions.checkArgument(jsonNode != null && jsonNode.isObject(),
+        "Cannot parse metadata update from non-object value: %s", jsonNode);
+    Preconditions.checkArgument(jsonNode.hasNonNull(ACTION), "Cannot parse metadata update. Missing field: action");
+    String action = JsonUtil.getString(ACTION, jsonNode).toLowerCase(Locale.ROOT);
+
+    switch (action) {
+      case ASSIGN_UUID:
+        throw new UnsupportedOperationException("Not implemented: AssignUUID");
+      case UPGRADE_FORMAT_VERSION:
+        return readAsUpgradeFormatVersion(jsonNode);

Review Comment:
   I will add the implementations for the other classes in a follow up PR after verifying that this is acceptable for others.



-- 
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-unsubscribe@iceberg.apache.org

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


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


[GitHub] [iceberg] kbendick commented on a diff in pull request #4601: [CORE] - Add MetadataUpdateParser and Jackson SerDe with implementation covering UpgradeFormatVersion

Posted by GitBox <gi...@apache.org>.
kbendick commented on code in PR #4601:
URL: https://github.com/apache/iceberg/pull/4601#discussion_r855646485


##########
core/src/main/java/org/apache/iceberg/MetadataUpdateParser.java:
##########
@@ -0,0 +1,160 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.iceberg;
+
+import com.fasterxml.jackson.core.JsonGenerator;
+import com.fasterxml.jackson.databind.JsonNode;
+import java.io.IOException;
+import java.io.StringWriter;
+import java.io.UncheckedIOException;
+import java.util.Locale;
+import java.util.Map;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap;
+import org.apache.iceberg.util.JsonUtil;
+
+public class MetadataUpdateParser {
+
+  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";
+
+  // UpgradeFormatVersion
+  private static final String FORMAT_VERSION = "format-version";
+
+  private static final Map<Class<? extends MetadataUpdate>, String> ACTIONS = ImmutableMap
+      .<Class<? extends MetadataUpdate>, String>builder()
+      .put(MetadataUpdate.AssignUUID.class, ASSIGN_UUID)
+      .put(MetadataUpdate.UpgradeFormatVersion.class, UPGRADE_FORMAT_VERSION)
+      .put(MetadataUpdate.AddSchema.class, ADD_SCHEMA)
+      .put(MetadataUpdate.SetCurrentSchema.class, SET_CURRENT_SCHEMA)
+      .put(MetadataUpdate.AddPartitionSpec.class, ADD_PARTITION_SPEC)
+      .put(MetadataUpdate.SetDefaultPartitionSpec.class, SET_DEFAULT_PARTITION_SPEC)
+      .put(MetadataUpdate.AddSortOrder.class, ADD_SORT_ORDER)
+      .put(MetadataUpdate.SetDefaultSortOrder.class, SET_DEFAULT_SORT_ORDER)
+      .put(MetadataUpdate.AddSnapshot.class, ADD_SNAPSHOT)
+      .put(MetadataUpdate.RemoveSnapshot.class, REMOVE_SNAPSHOTS)
+      .put(MetadataUpdate.SetSnapshotRef.class, SET_SNAPSHOT_REF)
+      .put(MetadataUpdate.SetProperties.class, SET_PROPERTIES)
+      .put(MetadataUpdate.RemoveProperties.class, REMOVE_PROPERTIES)
+      .put(MetadataUpdate.SetLocation.class, SET_LOCATION)
+      .build();
+
+  public static String toJson(MetadataUpdate metadataUpdate) {
+    return toJson(metadataUpdate, false);
+  }
+
+  public static String toJson(MetadataUpdate metadataUpdate, boolean pretty) {
+    try {
+      StringWriter writer = new StringWriter();
+      JsonGenerator generator = JsonUtil.factory().createGenerator(writer);
+      if (pretty) {
+        generator.useDefaultPrettyPrinter();
+      }
+      toJson(metadataUpdate, generator);
+      generator.flush();
+      return writer.toString();
+    } catch (IOException e) {
+      throw new UncheckedIOException(String.format("Failed to write metadata update json for: %s", metadataUpdate), e);
+    }
+  }
+
+  public static void toJson(MetadataUpdate metadataUpdate, JsonGenerator generator) throws IOException {
+    String updateAction = ACTIONS.get(metadataUpdate.getClass());

Review Comment:
   It does that more or less via the switch statement's default. But I'll add an explicit UnsupportedOperationException on `null`.



-- 
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-unsubscribe@iceberg.apache.org

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


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


[GitHub] [iceberg] rdblue commented on pull request #4601: [CORE] - Add MetadataUpdateParser and Jackson SerDe with implementation covering UpgradeFormatVersion

Posted by GitBox <gi...@apache.org>.
rdblue commented on PR #4601:
URL: https://github.com/apache/iceberg/pull/4601#issuecomment-1105833288

   Thanks! Looks like a great start.


-- 
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-unsubscribe@iceberg.apache.org

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


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


[GitHub] [iceberg] rdblue commented on a diff in pull request #4601: [CORE] - Add MetadataUpdateParser and Jackson SerDe with implementation covering UpgradeFormatVersion

Posted by GitBox <gi...@apache.org>.
rdblue commented on code in PR #4601:
URL: https://github.com/apache/iceberg/pull/4601#discussion_r855643712


##########
core/src/main/java/org/apache/iceberg/MetadataUpdateParser.java:
##########
@@ -0,0 +1,160 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.iceberg;
+
+import com.fasterxml.jackson.core.JsonGenerator;
+import com.fasterxml.jackson.databind.JsonNode;
+import java.io.IOException;
+import java.io.StringWriter;
+import java.io.UncheckedIOException;
+import java.util.Locale;
+import java.util.Map;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap;
+import org.apache.iceberg.util.JsonUtil;
+
+public class MetadataUpdateParser {
+
+  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";
+
+  // UpgradeFormatVersion
+  private static final String FORMAT_VERSION = "format-version";
+
+  private static final Map<Class<? extends MetadataUpdate>, String> ACTIONS = ImmutableMap
+      .<Class<? extends MetadataUpdate>, String>builder()
+      .put(MetadataUpdate.AssignUUID.class, ASSIGN_UUID)
+      .put(MetadataUpdate.UpgradeFormatVersion.class, UPGRADE_FORMAT_VERSION)
+      .put(MetadataUpdate.AddSchema.class, ADD_SCHEMA)
+      .put(MetadataUpdate.SetCurrentSchema.class, SET_CURRENT_SCHEMA)
+      .put(MetadataUpdate.AddPartitionSpec.class, ADD_PARTITION_SPEC)
+      .put(MetadataUpdate.SetDefaultPartitionSpec.class, SET_DEFAULT_PARTITION_SPEC)
+      .put(MetadataUpdate.AddSortOrder.class, ADD_SORT_ORDER)
+      .put(MetadataUpdate.SetDefaultSortOrder.class, SET_DEFAULT_SORT_ORDER)
+      .put(MetadataUpdate.AddSnapshot.class, ADD_SNAPSHOT)
+      .put(MetadataUpdate.RemoveSnapshot.class, REMOVE_SNAPSHOTS)
+      .put(MetadataUpdate.SetSnapshotRef.class, SET_SNAPSHOT_REF)
+      .put(MetadataUpdate.SetProperties.class, SET_PROPERTIES)
+      .put(MetadataUpdate.RemoveProperties.class, REMOVE_PROPERTIES)
+      .put(MetadataUpdate.SetLocation.class, SET_LOCATION)
+      .build();
+
+  public static String toJson(MetadataUpdate metadataUpdate) {
+    return toJson(metadataUpdate, false);
+  }
+
+  public static String toJson(MetadataUpdate metadataUpdate, boolean pretty) {
+    try {
+      StringWriter writer = new StringWriter();
+      JsonGenerator generator = JsonUtil.factory().createGenerator(writer);
+      if (pretty) {
+        generator.useDefaultPrettyPrinter();
+      }
+      toJson(metadataUpdate, generator);
+      generator.flush();
+      return writer.toString();
+    } catch (IOException e) {
+      throw new UncheckedIOException(String.format("Failed to write metadata update json for: %s", metadataUpdate), e);
+    }
+  }
+
+  public static void toJson(MetadataUpdate metadataUpdate, JsonGenerator generator) throws IOException {
+    String updateAction = ACTIONS.get(metadataUpdate.getClass());

Review Comment:
   I think this should check whether an action was found and throw an `UnsupportedOperationException` if it is not.



-- 
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-unsubscribe@iceberg.apache.org

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


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


[GitHub] [iceberg] rdblue merged pull request #4601: [CORE] - Add MetadataUpdateParser and Jackson SerDe with implementation covering UpgradeFormatVersion

Posted by GitBox <gi...@apache.org>.
rdblue merged PR #4601:
URL: https://github.com/apache/iceberg/pull/4601


-- 
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-unsubscribe@iceberg.apache.org

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


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


[GitHub] [iceberg] rdblue commented on a diff in pull request #4601: [CORE] - Add MetadataUpdateParser and Jackson SerDe with implementation covering UpgradeFormatVersion

Posted by GitBox <gi...@apache.org>.
rdblue commented on code in PR #4601:
URL: https://github.com/apache/iceberg/pull/4601#discussion_r855648565


##########
core/src/main/java/org/apache/iceberg/MetadataUpdateParser.java:
##########
@@ -0,0 +1,160 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.iceberg;
+
+import com.fasterxml.jackson.core.JsonGenerator;
+import com.fasterxml.jackson.databind.JsonNode;
+import java.io.IOException;
+import java.io.StringWriter;
+import java.io.UncheckedIOException;
+import java.util.Locale;
+import java.util.Map;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap;
+import org.apache.iceberg.util.JsonUtil;
+
+public class MetadataUpdateParser {
+
+  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";
+
+  // UpgradeFormatVersion
+  private static final String FORMAT_VERSION = "format-version";
+
+  private static final Map<Class<? extends MetadataUpdate>, String> ACTIONS = ImmutableMap
+      .<Class<? extends MetadataUpdate>, String>builder()
+      .put(MetadataUpdate.AssignUUID.class, ASSIGN_UUID)
+      .put(MetadataUpdate.UpgradeFormatVersion.class, UPGRADE_FORMAT_VERSION)
+      .put(MetadataUpdate.AddSchema.class, ADD_SCHEMA)
+      .put(MetadataUpdate.SetCurrentSchema.class, SET_CURRENT_SCHEMA)
+      .put(MetadataUpdate.AddPartitionSpec.class, ADD_PARTITION_SPEC)
+      .put(MetadataUpdate.SetDefaultPartitionSpec.class, SET_DEFAULT_PARTITION_SPEC)
+      .put(MetadataUpdate.AddSortOrder.class, ADD_SORT_ORDER)
+      .put(MetadataUpdate.SetDefaultSortOrder.class, SET_DEFAULT_SORT_ORDER)
+      .put(MetadataUpdate.AddSnapshot.class, ADD_SNAPSHOT)
+      .put(MetadataUpdate.RemoveSnapshot.class, REMOVE_SNAPSHOTS)
+      .put(MetadataUpdate.SetSnapshotRef.class, SET_SNAPSHOT_REF)
+      .put(MetadataUpdate.SetProperties.class, SET_PROPERTIES)
+      .put(MetadataUpdate.RemoveProperties.class, REMOVE_PROPERTIES)
+      .put(MetadataUpdate.SetLocation.class, SET_LOCATION)
+      .build();
+
+  public static String toJson(MetadataUpdate metadataUpdate) {
+    return toJson(metadataUpdate, false);
+  }
+
+  public static String toJson(MetadataUpdate metadataUpdate, boolean pretty) {
+    try {
+      StringWriter writer = new StringWriter();
+      JsonGenerator generator = JsonUtil.factory().createGenerator(writer);
+      if (pretty) {
+        generator.useDefaultPrettyPrinter();
+      }
+      toJson(metadataUpdate, generator);
+      generator.flush();
+      return writer.toString();
+    } catch (IOException e) {
+      throw new UncheckedIOException(String.format("Failed to write metadata update json for: %s", metadataUpdate), e);
+    }
+  }
+
+  public static void toJson(MetadataUpdate metadataUpdate, JsonGenerator generator) throws IOException {
+    String updateAction = ACTIONS.get(metadataUpdate.getClass());

Review Comment:
   Yeah, probably better to do it earlier, before passing it to the generator.



##########
core/src/main/java/org/apache/iceberg/MetadataUpdateParser.java:
##########
@@ -0,0 +1,160 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.iceberg;
+
+import com.fasterxml.jackson.core.JsonGenerator;
+import com.fasterxml.jackson.databind.JsonNode;
+import java.io.IOException;
+import java.io.StringWriter;
+import java.io.UncheckedIOException;
+import java.util.Locale;
+import java.util.Map;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap;
+import org.apache.iceberg.util.JsonUtil;
+
+public class MetadataUpdateParser {
+
+  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";
+
+  // UpgradeFormatVersion
+  private static final String FORMAT_VERSION = "format-version";
+
+  private static final Map<Class<? extends MetadataUpdate>, String> ACTIONS = ImmutableMap
+      .<Class<? extends MetadataUpdate>, String>builder()
+      .put(MetadataUpdate.AssignUUID.class, ASSIGN_UUID)
+      .put(MetadataUpdate.UpgradeFormatVersion.class, UPGRADE_FORMAT_VERSION)
+      .put(MetadataUpdate.AddSchema.class, ADD_SCHEMA)
+      .put(MetadataUpdate.SetCurrentSchema.class, SET_CURRENT_SCHEMA)
+      .put(MetadataUpdate.AddPartitionSpec.class, ADD_PARTITION_SPEC)
+      .put(MetadataUpdate.SetDefaultPartitionSpec.class, SET_DEFAULT_PARTITION_SPEC)
+      .put(MetadataUpdate.AddSortOrder.class, ADD_SORT_ORDER)
+      .put(MetadataUpdate.SetDefaultSortOrder.class, SET_DEFAULT_SORT_ORDER)
+      .put(MetadataUpdate.AddSnapshot.class, ADD_SNAPSHOT)
+      .put(MetadataUpdate.RemoveSnapshot.class, REMOVE_SNAPSHOTS)
+      .put(MetadataUpdate.SetSnapshotRef.class, SET_SNAPSHOT_REF)
+      .put(MetadataUpdate.SetProperties.class, SET_PROPERTIES)
+      .put(MetadataUpdate.RemoveProperties.class, REMOVE_PROPERTIES)
+      .put(MetadataUpdate.SetLocation.class, SET_LOCATION)
+      .build();
+
+  public static String toJson(MetadataUpdate metadataUpdate) {
+    return toJson(metadataUpdate, false);
+  }
+
+  public static String toJson(MetadataUpdate metadataUpdate, boolean pretty) {
+    try {
+      StringWriter writer = new StringWriter();
+      JsonGenerator generator = JsonUtil.factory().createGenerator(writer);
+      if (pretty) {
+        generator.useDefaultPrettyPrinter();
+      }
+      toJson(metadataUpdate, generator);
+      generator.flush();
+      return writer.toString();
+    } catch (IOException e) {
+      throw new UncheckedIOException(String.format("Failed to write metadata update json for: %s", metadataUpdate), e);
+    }
+  }
+
+  public static void toJson(MetadataUpdate metadataUpdate, JsonGenerator generator) throws IOException {
+    String updateAction = ACTIONS.get(metadataUpdate.getClass());
+
+    // Add shared `action` field used to differentiate between implementations of MetadataUpdate
+    generator.writeStartObject();
+    generator.writeStringField(ACTION, updateAction);
+
+    // Write subclass specific fields
+    switch (updateAction) {
+      case ASSIGN_UUID:
+        throw new UnsupportedOperationException("Not Implemented: MetadataUpdate#toJson for AssignUUID");
+      case UPGRADE_FORMAT_VERSION:
+        writeUpgradeFormatVersion((MetadataUpdate.UpgradeFormatVersion) metadataUpdate, generator);
+        break;
+      default:
+        throw new IllegalArgumentException(

Review Comment:
   Should this also be `UnsupportedOperationException`?



-- 
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-unsubscribe@iceberg.apache.org

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


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


[GitHub] [iceberg] rdblue commented on a diff in pull request #4601: [CORE] - Add MetadataUpdateParser and Jackson SerDe with implementation covering UpgradeFormatVersion

Posted by GitBox <gi...@apache.org>.
rdblue commented on code in PR #4601:
URL: https://github.com/apache/iceberg/pull/4601#discussion_r855644757


##########
core/src/main/java/org/apache/iceberg/MetadataUpdateParser.java:
##########
@@ -0,0 +1,160 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.iceberg;
+
+import com.fasterxml.jackson.core.JsonGenerator;
+import com.fasterxml.jackson.databind.JsonNode;
+import java.io.IOException;
+import java.io.StringWriter;
+import java.io.UncheckedIOException;
+import java.util.Locale;
+import java.util.Map;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap;
+import org.apache.iceberg.util.JsonUtil;
+
+public class MetadataUpdateParser {
+
+  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";
+
+  // UpgradeFormatVersion
+  private static final String FORMAT_VERSION = "format-version";
+
+  private static final Map<Class<? extends MetadataUpdate>, String> ACTIONS = ImmutableMap
+      .<Class<? extends MetadataUpdate>, String>builder()
+      .put(MetadataUpdate.AssignUUID.class, ASSIGN_UUID)
+      .put(MetadataUpdate.UpgradeFormatVersion.class, UPGRADE_FORMAT_VERSION)
+      .put(MetadataUpdate.AddSchema.class, ADD_SCHEMA)
+      .put(MetadataUpdate.SetCurrentSchema.class, SET_CURRENT_SCHEMA)
+      .put(MetadataUpdate.AddPartitionSpec.class, ADD_PARTITION_SPEC)
+      .put(MetadataUpdate.SetDefaultPartitionSpec.class, SET_DEFAULT_PARTITION_SPEC)
+      .put(MetadataUpdate.AddSortOrder.class, ADD_SORT_ORDER)
+      .put(MetadataUpdate.SetDefaultSortOrder.class, SET_DEFAULT_SORT_ORDER)
+      .put(MetadataUpdate.AddSnapshot.class, ADD_SNAPSHOT)
+      .put(MetadataUpdate.RemoveSnapshot.class, REMOVE_SNAPSHOTS)
+      .put(MetadataUpdate.SetSnapshotRef.class, SET_SNAPSHOT_REF)
+      .put(MetadataUpdate.SetProperties.class, SET_PROPERTIES)
+      .put(MetadataUpdate.RemoveProperties.class, REMOVE_PROPERTIES)
+      .put(MetadataUpdate.SetLocation.class, SET_LOCATION)
+      .build();
+
+  public static String toJson(MetadataUpdate metadataUpdate) {
+    return toJson(metadataUpdate, false);
+  }
+
+  public static String toJson(MetadataUpdate metadataUpdate, boolean pretty) {
+    try {
+      StringWriter writer = new StringWriter();
+      JsonGenerator generator = JsonUtil.factory().createGenerator(writer);
+      if (pretty) {
+        generator.useDefaultPrettyPrinter();
+      }
+      toJson(metadataUpdate, generator);
+      generator.flush();
+      return writer.toString();
+    } catch (IOException e) {
+      throw new UncheckedIOException(String.format("Failed to write metadata update json for: %s", metadataUpdate), e);
+    }
+  }
+
+  public static void toJson(MetadataUpdate metadataUpdate, JsonGenerator generator) throws IOException {
+    String updateAction = ACTIONS.get(metadataUpdate.getClass());
+
+    // Add shared `action` field used to differentiate between implementations of MetadataUpdate
+    generator.writeStartObject();
+    generator.writeStringField(ACTION, updateAction);
+
+    // Write subclass specific fields
+    switch (updateAction) {
+      case ASSIGN_UUID:
+        throw new UnsupportedOperationException("Not Implemented: MetadataUpdate#toJson for AssignUUID");
+      case UPGRADE_FORMAT_VERSION:
+        writeUpgradeFormatVersion((MetadataUpdate.UpgradeFormatVersion) metadataUpdate, generator);
+        break;
+      default:
+        throw new IllegalArgumentException(
+            String.format("Cannot convert metadata update to json. Unrecognized action: %s", updateAction));
+    }
+
+    generator.writeEndObject();
+  }
+
+  /**
+   * Read MetadataUpdate from a JSON string.
+   *
+   * @param json a JSON string of a MetadataUpdate
+   * @return a MetadataUpdate object
+   */
+  public static MetadataUpdate fromJson(String json) {
+    try {
+      return fromJson(JsonUtil.mapper().readValue(json, JsonNode.class));
+    } catch (IOException e) {
+      throw new UncheckedIOException("Failed to read JSON string: " + json, e);
+    }
+  }
+
+  public static MetadataUpdate fromJson(JsonNode jsonNode) {
+    Preconditions.checkArgument(jsonNode != null && jsonNode.isObject(),
+        "Cannot parse metadata update from non-object value: %s", jsonNode);
+    Preconditions.checkArgument(jsonNode.hasNonNull(ACTION), "Cannot parse metadata update. Missing field: action");
+    String action = JsonUtil.getString(ACTION, jsonNode).toLowerCase(Locale.ROOT);
+
+    switch (action) {
+      case ASSIGN_UUID:
+        throw new UnsupportedOperationException("Not implemented: AssignUUID");
+      case UPGRADE_FORMAT_VERSION:
+        return readAsUpgradeFormatVersion(jsonNode);
+      default:
+        throw new UnsupportedOperationException(
+            String.format("Cannot convert metadata update action to json: %s", action));
+    }
+  }
+
+  // Write all fields specific to UpgradeFormatVersion

Review Comment:
   Nit: This comment seems to duplicate what we know from the method name.



-- 
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-unsubscribe@iceberg.apache.org

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


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