rdblue commented on code in PR #6559:
URL: https://github.com/apache/iceberg/pull/6559#discussion_r1174654668


##########
core/src/main/java/org/apache/iceberg/view/ViewMetadataParser.java:
##########
@@ -0,0 +1,213 @@
+/*
+ * 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.view;
+
+import com.fasterxml.jackson.core.JsonGenerator;
+import com.fasterxml.jackson.databind.JsonNode;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.nio.charset.StandardCharsets;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import org.apache.iceberg.Schema;
+import org.apache.iceberg.SchemaParser;
+import org.apache.iceberg.exceptions.RuntimeIOException;
+import org.apache.iceberg.io.InputFile;
+import org.apache.iceberg.io.OutputFile;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList;
+import org.apache.iceberg.relocated.com.google.common.collect.Lists;
+import org.apache.iceberg.util.JsonUtil;
+import org.apache.iceberg.util.PropertyUtil;
+
+class ViewMetadataParser {
+
+  // visible for testing
+  static final String FORMAT_VERSION = "format-version";
+  static final String LOCATION = "location";
+  static final String CURRENT_VERSION_ID = "current-version-id";
+  static final String VERSIONS = "versions";
+  static final String VERSION_LOG = "version-log";
+  static final String PROPERTIES = "properties";
+  static final String SCHEMAS = "schemas";
+  static final String CURRENT_SCHEMA_ID = "current-schema-id";
+
+  public static void overwrite(ViewMetadata metadata, OutputFile outputFile) {
+    internalWrite(metadata, outputFile, true);
+  }
+
+  public static void write(ViewMetadata metadata, OutputFile outputFile) {
+    internalWrite(metadata, outputFile, false);
+  }
+
+  public static void internalWrite(
+      ViewMetadata metadata, OutputFile outputFile, boolean overwrite) {
+    OutputStream stream = overwrite ? outputFile.createOrOverwrite() : 
outputFile.create();
+    try (OutputStreamWriter writer = new OutputStreamWriter(stream, 
StandardCharsets.UTF_8)) {
+      JsonGenerator generator = JsonUtil.factory().createGenerator(writer);
+      generator.useDefaultPrettyPrinter();
+      toJson(metadata, generator);
+      generator.flush();
+    } catch (IOException e) {
+      throw new RuntimeIOException(e, "Failed to write json to file: %s", 
outputFile);
+    }
+  }
+
+  public static void toJson(ViewMetadata metadata, JsonGenerator generator) 
throws IOException {
+    generator.writeStartObject();
+
+    generator.writeNumberField(FORMAT_VERSION, metadata.formatVersion());
+    generator.writeStringField(LOCATION, metadata.location());
+
+    JsonUtil.writeStringMap(PROPERTIES, metadata.properties(), generator);
+
+    if (metadata.schemas() != null && !metadata.schemas().isEmpty()) {
+      generator.writeNumberField(CURRENT_SCHEMA_ID, 
metadata.currentSchemaId());
+      generator.writeArrayFieldStart(SCHEMAS);
+      for (Schema schema : metadata.schemas()) {
+        SchemaParser.toJson(schema, generator);
+      }
+
+      generator.writeEndArray();
+    }

Review Comment:
   I think it is optional for convenience. But I don't think that's a very 
strong reason.
   
   It would be strange for a view to not have a schema when you inspect it. And 
it would similarly be unexpected for a view to be created directly by a human 
rather than an engine. I think it makes sense to require the `schemas`.
   
   However, I would also make a couple of other changes. First, 
`current-schema-id` doesn't make sense because it would need to be maintained 
whenever the view version changes. Why keep it in sync when we could just drop 
the field and use the current version's schema?
   
   Also, I don't think that the view version tracks a schema right now, but 
since we expect all representations to produce the same schema, I think that it 
should. I'd probably move `schema-id` from the SQL representation to the view 
version.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to