nastra commented on code in PR #6559: URL: https://github.com/apache/iceberg/pull/6559#discussion_r1187069830
########## core/src/main/java/org/apache/iceberg/view/ViewMetadataParser.java: ########## @@ -0,0 +1,227 @@ +/* + * 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.io.UncheckedIOException; +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.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 { + + 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 toJson(ViewMetadata metadata, JsonGenerator generator) throws IOException { Review Comment: I think we should check for null on `metadata` + add a test ########## core/src/main/java/org/apache/iceberg/view/ViewMetadataParser.java: ########## @@ -0,0 +1,227 @@ +/* + * 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.io.UncheckedIOException; +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.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 { + + 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 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); + generator.writeNumberField(CURRENT_SCHEMA_ID, metadata.currentSchemaId()); + generator.writeArrayFieldStart(SCHEMAS); + for (Schema schema : metadata.schemas()) { + SchemaParser.toJson(schema, generator); + } + + generator.writeEndArray(); + + generator.writeNumberField(CURRENT_VERSION_ID, metadata.currentVersionId()); + generator.writeArrayFieldStart(VERSIONS); + for (ViewVersion version : metadata.versions()) { + ViewVersionParser.toJson(version, generator); + } + + generator.writeEndArray(); + + generator.writeArrayFieldStart(VERSION_LOG); + for (ViewHistoryEntry viewHistoryEntry : metadata.history()) { + ViewHistoryEntryParser.toJson(viewHistoryEntry, generator); + } + + generator.writeEndArray(); + generator.writeEndObject(); + } + + static String toJson(ViewMetadata viewMetadata) { + return JsonUtil.generate(gen -> toJson(viewMetadata, gen), false); + } + + public static ViewMetadata read(InputFile file) { + try (InputStream is = file.newStream()) { + return fromJson(JsonUtil.mapper().readValue(is, JsonNode.class)); + } catch (IOException e) { + throw new UncheckedIOException(String.format("Failed to read json file: %s", file), e); + } + } + + public static ViewMetadata fromJson(JsonNode node) { + Preconditions.checkArgument(node != null, "Cannot parse view metadata from null json"); Review Comment: would be good to add a test for this ########## core/src/test/java/org/apache/iceberg/view/TestViewMetadata.java: ########## @@ -0,0 +1,174 @@ +/* + * 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 java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Arrays; +import java.util.Collection; +import org.apache.iceberg.Schema; +import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList; +import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap; +import org.apache.iceberg.types.Types; +import org.assertj.core.api.Assertions; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runners.Parameterized; + +public class TestViewMetadata { Review Comment: should this be called `TestViewMetadataParser` instead? ########## core/src/test/java/org/apache/iceberg/view/TestViewMetadata.java: ########## @@ -0,0 +1,174 @@ +/* + * 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 java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Arrays; +import java.util.Collection; +import org.apache.iceberg.Schema; +import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList; +import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap; +import org.apache.iceberg.types.Types; +import org.assertj.core.api.Assertions; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runners.Parameterized; + +public class TestViewMetadata { + + private static ViewVersion version1 = + ImmutableViewVersion.builder() + .versionId(1) + .timestampMillis(4353L) + .summary(ImmutableMap.of("operation", "create")) + .schemaId(1) + .addRepresentations( + ImmutableSQLViewRepresentation.builder() + .sql("select 'foo' foo") + .dialect("spark-sql") + .defaultCatalog("some-catalog") + .build()) + .build(); + + private static ViewHistoryEntry historyEntry1 = + ImmutableViewHistoryEntry.builder().timestampMillis(4353L).versionId(1).build(); + + private static ViewVersion version2 = + ImmutableViewVersion.builder() + .versionId(2) + .schemaId(1) + .timestampMillis(5555L) + .summary(ImmutableMap.of("operation", "replace")) + .addRepresentations( + ImmutableSQLViewRepresentation.builder() + .sql("select 1 id, 'abc' data") + .defaultCatalog("some-catalog") + .dialect("spark-sql") + .build()) + .build(); + + private static ViewHistoryEntry historyEntry2 = + ImmutableViewHistoryEntry.builder().timestampMillis(5555L).versionId(2).build(); + + private static final Schema TEST_SCHEMA = + new Schema( + 1, + Types.NestedField.required(1, "x", Types.LongType.get()), + Types.NestedField.required(2, "y", Types.LongType.get(), "comment"), + Types.NestedField.required(3, "z", Types.LongType.get())); + + @Parameterized.Parameters + public static Collection<Object[]> parameters() { + return Arrays.asList(new Object[][] {}); + } + + @Test + public void testReadAndWriteValidViewMetadata() throws Exception { + String json = readViewMetadataInputFile("view/ValidViewMetadata.json"); + ViewMetadata expectedViewMetadata = + ImmutableViewMetadata.builder() + .currentSchemaId(1) + .schemas(ImmutableList.of(TEST_SCHEMA)) + .versions(ImmutableList.of(version1, version2)) + .history(ImmutableList.of(historyEntry1, historyEntry2)) + .location("s3://bucket/test/location") + .properties(ImmutableMap.of("some-key", "some-value")) + .currentVersionId(2) + .formatVersion(1) + .build(); + assertSameViewMetadata(expectedViewMetadata, ViewMetadataParser.fromJson(json)); Review Comment: nit: `assertSame` usually suggests that there's some sort of identity comparison between two objects. ########## core/src/main/java/org/apache/iceberg/view/ViewMetadata.java: ########## @@ -0,0 +1,126 @@ +/* + * 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 java.io.Serializable; +import java.util.Comparator; +import java.util.List; +import java.util.Map; +import javax.annotation.Nullable; +import org.apache.iceberg.Schema; +import org.apache.iceberg.relocated.com.google.common.base.Preconditions; +import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap; +import org.apache.iceberg.relocated.com.google.common.collect.Ordering; +import org.immutables.value.Value; + [email protected] +public abstract class ViewMetadata implements Serializable { + static final int DEFAULT_VIEW_FORMAT_VERSION = 1; + static final int SUPPORTED_VIEW_FORMAT_VERSION = 1; + + public static ImmutableViewMetadata.Builder buildFrom(ViewMetadata metadata) { Review Comment: I think what @jackye1995 means here is to move this method to the bottom after all the API definitions (`formatVersion()` / `location()` / ...) ########## core/src/main/java/org/apache/iceberg/view/ViewMetadataParser.java: ########## @@ -0,0 +1,227 @@ +/* + * 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.io.UncheckedIOException; +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.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 { + + 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 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); + generator.writeNumberField(CURRENT_SCHEMA_ID, metadata.currentSchemaId()); + generator.writeArrayFieldStart(SCHEMAS); + for (Schema schema : metadata.schemas()) { + SchemaParser.toJson(schema, generator); + } + + generator.writeEndArray(); + + generator.writeNumberField(CURRENT_VERSION_ID, metadata.currentVersionId()); + generator.writeArrayFieldStart(VERSIONS); + for (ViewVersion version : metadata.versions()) { + ViewVersionParser.toJson(version, generator); + } + + generator.writeEndArray(); + + generator.writeArrayFieldStart(VERSION_LOG); + for (ViewHistoryEntry viewHistoryEntry : metadata.history()) { + ViewHistoryEntryParser.toJson(viewHistoryEntry, generator); + } + + generator.writeEndArray(); + generator.writeEndObject(); + } + + static String toJson(ViewMetadata viewMetadata) { + return JsonUtil.generate(gen -> toJson(viewMetadata, gen), false); + } + + public static ViewMetadata read(InputFile file) { + try (InputStream is = file.newStream()) { + return fromJson(JsonUtil.mapper().readValue(is, JsonNode.class)); + } catch (IOException e) { + throw new UncheckedIOException(String.format("Failed to read json file: %s", file), e); + } + } + + public static ViewMetadata fromJson(JsonNode node) { + Preconditions.checkArgument(node != null, "Cannot parse view metadata from null json"); + Preconditions.checkArgument( + node.isObject(), "Cannot parse metadata from a non-object: %s", node); + + int formatVersion = JsonUtil.getInt(FORMAT_VERSION, node); + Preconditions.checkArgument( + formatVersion <= ViewMetadata.SUPPORTED_VIEW_FORMAT_VERSION, + "Cannot read unsupported version %s", + formatVersion); + + String location = JsonUtil.getString(LOCATION, node); + int currentVersionId = JsonUtil.getInt(CURRENT_VERSION_ID, node); + Map<String, String> properties = JsonUtil.getStringMap(PROPERTIES, node); + + JsonNode versionsListNode = JsonUtil.get(VERSIONS, node); + Preconditions.checkArgument( + versionsListNode.isArray(), "Cannot parse versions from non-array: %s", versionsListNode); + + List<ViewVersion> versions = Lists.newArrayListWithExpectedSize(versionsListNode.size()); + ViewVersion currentVersion = null; + for (JsonNode versionNode : versionsListNode) { + ViewVersion version = ViewVersionParser.fromJson(versionNode); + if (version.versionId() == currentVersionId) { + currentVersion = version; + } + + versions.add(version); + } + + Preconditions.checkArgument( + currentVersion != null, + "Cannot find version with %s=%s from %s", + CURRENT_VERSION_ID, + currentVersionId, + VERSIONS); + + JsonNode versionLogNode = JsonUtil.get(VERSION_LOG, node); + Preconditions.checkArgument( + versionLogNode.isArray(), "Cannot parse version-log from non-array: %s", versionLogNode); + List<ViewHistoryEntry> historyEntries = + Lists.newArrayListWithExpectedSize(versionLogNode.size()); + Iterator<JsonNode> versionLogIterator = versionLogNode.elements(); + while (versionLogIterator.hasNext()) { + historyEntries.add(ViewHistoryEntryParser.fromJson(versionLogIterator.next())); + } + + List<Schema> schemas; + Integer currentSchemaId; + Schema currentSchema = null; + JsonNode schemaArray = node.get(SCHEMAS); + + Preconditions.checkArgument( + schemaArray.isArray(), "Cannot parse schemas from non-array: %s", schemaArray); + currentSchemaId = JsonUtil.getInt(CURRENT_SCHEMA_ID, node); + + ImmutableList.Builder<Schema> builder = ImmutableList.builder(); + for (JsonNode schemaNode : schemaArray) { + Schema schema = SchemaParser.fromJson(schemaNode); + if (schema.schemaId() == currentSchemaId) { + currentSchema = schema; + } + + builder.add(schema); + } + + Preconditions.checkArgument( + currentSchema != null, + "Cannot find schema with %s=%s from %s", + CURRENT_SCHEMA_ID, + currentSchemaId, + SCHEMAS); + + schemas = builder.build(); + + int numVersionsToKeep = + PropertyUtil.propertyAsInt( + properties, + ViewProperties.VERSION_HISTORY_SIZE, + ViewProperties.VERSION_HISTORY_SIZE_DEFAULT); + + Preconditions.checkArgument( + numVersionsToKeep >= 1, "%s must be positive", ViewProperties.VERSION_HISTORY_SIZE); + + if (versions.size() > numVersionsToKeep) { + versions = versions.subList(versions.size() - numVersionsToKeep, versions.size()); + historyEntries = + historyEntries.subList(historyEntries.size() - numVersionsToKeep, historyEntries.size()); + } + + return ImmutableViewMetadata.builder() + .location(location) + .currentVersionId(currentVersionId) + .properties(properties) + .versions(versions) + .schemas(schemas) + .currentSchemaId(currentSchemaId) + .history(historyEntries) + .formatVersion(formatVersion) + .build(); + } + + static ViewMetadata fromJson(String json) { + Preconditions.checkArgument(json != null, "Cannot parse view metadata from null string"); + return JsonUtil.parse(json, ViewMetadataParser::fromJson); + } + + private 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 UncheckedIOException( + String.format("Failed to write json to file: %s", outputFile), e); + } + } + + private ViewMetadataParser() {} Review Comment: nit: I think this should be at the top ########## core/src/main/java/org/apache/iceberg/view/ViewMetadata.java: ########## @@ -0,0 +1,112 @@ +/* + * 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 java.io.Serializable; +import java.util.List; +import java.util.Map; +import org.apache.iceberg.Schema; +import org.apache.iceberg.relocated.com.google.common.base.Preconditions; +import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap; +import org.immutables.value.Value; + [email protected] +public abstract class ViewMetadata implements Serializable { + static final int DEFAULT_VIEW_FORMAT_VERSION = 1; + static final int SUPPORTED_VIEW_FORMAT_VERSION = 1; + + public static ImmutableViewMetadata.Builder buildFrom(ViewMetadata metadata) { + return ImmutableViewMetadata.builder() + .formatVersion(DEFAULT_VIEW_FORMAT_VERSION) + .location(metadata.location()) + .properties(metadata.properties()) + .currentVersionId(metadata.currentVersionId()) + .versions(metadata.versions()) + .schemas(metadata.schemas()) + .currentSchemaId(metadata.currentSchemaId()) + .history(metadata.history()); + } + + @Value.Check + void check() { + Preconditions.checkState(versions().size() > 0, "Expecting non-empty version log"); + } + + public abstract int formatVersion(); + + public abstract String location(); + + public abstract Map<String, String> properties(); + + public abstract int currentVersionId(); + + public abstract List<ViewVersion> versions(); + + public abstract List<ViewHistoryEntry> history(); + + public abstract List<Schema> schemas(); + + public abstract Integer currentSchemaId(); + + public ViewVersion version(int versionId) { + return versionsById().get(versionId); + } + + @Value.Derived + public ViewVersion currentVersion() { + return versionsById().get(currentVersionId()); + } + + @Value.Derived + public Map<Integer, ViewVersion> versionsById() { + return indexVersions(versions()); + } + + @Value.Derived + public Map<Integer, Schema> schemasById() { + return indexSchemas(schemas()); + } + + @Value.Derived + public Schema schema() { + return schemasById().get(currentSchemaId()); + } + + private static Map<Integer, ViewVersion> indexVersions(List<ViewVersion> versions) { Review Comment: nit: I think `indexVersions` and `indexSchemas` don't need to be `static` ########## core/src/main/java/org/apache/iceberg/view/ViewMetadata.java: ########## @@ -0,0 +1,112 @@ +/* + * 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 java.io.Serializable; +import java.util.List; +import java.util.Map; +import org.apache.iceberg.Schema; +import org.apache.iceberg.relocated.com.google.common.base.Preconditions; +import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap; +import org.immutables.value.Value; + [email protected] +public abstract class ViewMetadata implements Serializable { + static final int DEFAULT_VIEW_FORMAT_VERSION = 1; + static final int SUPPORTED_VIEW_FORMAT_VERSION = 1; + + public static ImmutableViewMetadata.Builder buildFrom(ViewMetadata metadata) { Review Comment: is this used anywhere? Also we can probably just use `ImmutableViewMetadata.builder().from(metadata).xyz().build()` here ########## core/src/test/java/org/apache/iceberg/view/TestViewMetadata.java: ########## @@ -0,0 +1,174 @@ +/* + * 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 java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Arrays; +import java.util.Collection; +import org.apache.iceberg.Schema; +import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList; +import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap; +import org.apache.iceberg.types.Types; +import org.assertj.core.api.Assertions; +import org.junit.Assert; +import org.junit.Test; Review Comment: I think it's worth changing this new test to using JUnit5 ########## core/src/main/java/org/apache/iceberg/view/ViewMetadataParser.java: ########## @@ -0,0 +1,227 @@ +/* + * 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.io.UncheckedIOException; +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.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 { + + 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 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); + generator.writeNumberField(CURRENT_SCHEMA_ID, metadata.currentSchemaId()); + generator.writeArrayFieldStart(SCHEMAS); + for (Schema schema : metadata.schemas()) { + SchemaParser.toJson(schema, generator); + } + + generator.writeEndArray(); + + generator.writeNumberField(CURRENT_VERSION_ID, metadata.currentVersionId()); + generator.writeArrayFieldStart(VERSIONS); + for (ViewVersion version : metadata.versions()) { + ViewVersionParser.toJson(version, generator); + } + + generator.writeEndArray(); + + generator.writeArrayFieldStart(VERSION_LOG); + for (ViewHistoryEntry viewHistoryEntry : metadata.history()) { + ViewHistoryEntryParser.toJson(viewHistoryEntry, generator); + } + + generator.writeEndArray(); + generator.writeEndObject(); + } + + static String toJson(ViewMetadata viewMetadata) { Review Comment: nit: maybe move this before the other `toJson(..)` method ########## core/src/main/java/org/apache/iceberg/view/ViewMetadata.java: ########## @@ -0,0 +1,112 @@ +/* + * 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 java.io.Serializable; +import java.util.List; +import java.util.Map; +import org.apache.iceberg.Schema; +import org.apache.iceberg.relocated.com.google.common.base.Preconditions; +import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap; +import org.immutables.value.Value; + [email protected] +public abstract class ViewMetadata implements Serializable { + static final int DEFAULT_VIEW_FORMAT_VERSION = 1; + static final int SUPPORTED_VIEW_FORMAT_VERSION = 1; + + public static ImmutableViewMetadata.Builder buildFrom(ViewMetadata metadata) { Review Comment: or is the goal here to just create a copy? In that case we can also just do `ImmutableViewMetadata.copyOf(metadata)` -- 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]
