nastra commented on code in PR #9728: URL: https://github.com/apache/iceberg/pull/9728#discussion_r1583006389
########## core/src/test/java/org/apache/iceberg/TestDataTaskParser.java: ########## @@ -0,0 +1,249 @@ +/* + * 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 static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import java.io.StringWriter; +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap; +import org.apache.iceberg.relocated.com.google.common.collect.Lists; +import org.apache.iceberg.util.JsonUtil; +import org.junit.jupiter.api.Test; + +public class TestDataTaskParser { + @Test + public void nullCheck() throws Exception { + StringWriter writer = new StringWriter(); + JsonGenerator generator = JsonUtil.factory().createGenerator(writer); + + assertThatThrownBy(() -> DataTaskParser.toJson(null, generator)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Invalid data task: null"); + + assertThatThrownBy(() -> DataTaskParser.toJson((StaticDataTask) createDataTask(), null)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Invalid JSON generator: null"); + + assertThatThrownBy(() -> DataTaskParser.fromJson(null)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Invalid JSON node for data task: null"); + } + + @Test + public void invalidJsonNode() throws Exception { + String jsonStr = "{\"str\":\"1\", \"arr\":[]}"; + ObjectMapper mapper = new ObjectMapper(); + JsonNode rootNode = mapper.reader().readTree(jsonStr); + + assertThatThrownBy(() -> DataTaskParser.fromJson(rootNode.get("str"))) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("Invalid JSON node for data task: non-object "); + + assertThatThrownBy(() -> DataTaskParser.fromJson(rootNode.get("arr"))) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("Invalid JSON node for data task: non-object "); + } + + @Test + public void missingFields() throws Exception { + ObjectMapper mapper = new ObjectMapper(); + + String missingSchemaStr = "{}"; + JsonNode missingSchemaNode = mapper.reader().readTree(missingSchemaStr); + assertThatThrownBy(() -> DataTaskParser.fromJson(missingSchemaNode)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("Cannot parse missing field: schema"); + + String missingProjectionStr = + "{" + + "\"schema\":{\"type\":\"struct\",\"schema-id\":0," + + "\"fields\":[{\"id\":1,\"name\":\"committed_at\",\"required\":true,\"type\":\"timestamptz\"}]}" + + "}"; + JsonNode missingProjectionNode = mapper.reader().readTree(missingProjectionStr); + assertThatThrownBy(() -> DataTaskParser.fromJson(missingProjectionNode)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("Cannot parse missing field: projection"); + + String missingMetadataFileStr = + "{" + + "\"schema\":{\"type\":\"struct\",\"schema-id\":0," + + "\"fields\":[{\"id\":1,\"name\":\"committed_at\",\"required\":true,\"type\":\"timestamptz\"}]}," + + "\"projection\":{\"type\":\"struct\",\"schema-id\":0," + + "\"fields\":[{\"id\":1,\"name\":\"committed_at\",\"required\":true,\"type\":\"timestamptz\"}]}" + + "}"; + JsonNode missingMetadataFileNode = mapper.reader().readTree(missingMetadataFileStr); + assertThatThrownBy(() -> DataTaskParser.fromJson(missingMetadataFileNode)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("Cannot parse missing field: metadata-file"); + + String missingTableRowsStr = + "{\"task-type\":\"data-task\"," + + "\"schema\":{\"type\":\"struct\",\"schema-id\":0," + + "\"fields\":[{\"id\":1,\"name\":\"committed_at\",\"required\":true,\"type\":\"timestamptz\"}," + + "{\"id\":2,\"name\":\"snapshot_id\",\"required\":true,\"type\":\"long\"}," + + "{\"id\":3,\"name\":\"parent_id\",\"required\":false,\"type\":\"long\"}," + + "{\"id\":4,\"name\":\"operation\",\"required\":false,\"type\":\"string\"}," + + "{\"id\":5,\"name\":\"manifest_list\",\"required\":false,\"type\":\"string\"}," + + "{\"id\":6,\"name\":\"summary\",\"required\":false,\"type\":{\"type\":\"map\"," + + "\"key-id\":7,\"key\":\"string\",\"value-id\":8," + + "\"value\":\"string\",\"value-required\":true}}]}," + + "\"projection\":{\"type\":\"struct\",\"schema-id\":0," + + "\"fields\":[{\"id\":1,\"name\":\"committed_at\",\"required\":true,\"type\":\"timestamptz\"}," + + "{\"id\":2,\"name\":\"snapshot_id\",\"required\":true,\"type\":\"long\"}," + + "{\"id\":3,\"name\":\"parent_id\",\"required\":false,\"type\":\"long\"}," + + "{\"id\":4,\"name\":\"operation\",\"required\":false,\"type\":\"string\"}," + + "{\"id\":5,\"name\":\"manifest_list\",\"required\":false,\"type\":\"string\"}," + + "{\"id\":6,\"name\":\"summary\",\"required\":false,\"type\":{\"type\":\"map\"," + + "\"key-id\":7,\"key\":\"string\",\"value-id\":8," + + "\"value\":\"string\",\"value-required\":true}}]}," + + "\"metadata-file\":{\"spec-id\":0,\"content\":\"DATA\"," + + "\"file-path\":\"/tmp/metadata2.json\"," + + "\"file-format\":\"METADATA\",\"partition\":{}," + + "\"file-size-in-bytes\":0,\"record-count\":2,\"sort-order-id\":0}" + + "}"; + JsonNode missingTableRowsNode = mapper.reader().readTree(missingTableRowsStr); + assertThatThrownBy(() -> DataTaskParser.fromJson(missingTableRowsNode)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("Cannot parse missing field: rows"); + } + + @Test + public void roundTripSerde() { + StaticDataTask dataTask = (StaticDataTask) createDataTask(); + String jsonStr = ScanTaskParser.toJson(dataTask); + assertThat(jsonStr).isEqualTo(snapshotsDataTaskJson()); + StaticDataTask deserializedTask = (StaticDataTask) ScanTaskParser.fromJson(jsonStr, true); + assertDataTaskEquals(dataTask, deserializedTask); + } + + private DataTask createDataTask() { + Map<String, String> summary1 = + ImmutableMap.of( + "added-data-files", "1", + "added-records", "1", + "added-files-size", "10", + "changed-partition-count", "1", + "total-records", "1", + "total-files-size", "10", + "total-data-files", "1", + "total-delete-files", "0", + "total-position-deletes", "0", + "total-equality-deletes", "0"); + + Map<String, String> summary2 = + ImmutableMap.of( + "added-data-files", "1", + "added-records", "1", + "added-files-size", "10", + "changed-partition-count", "1", + "total-records", "2", + "total-files-size", "20", + "total-data-files", "2", + "total-delete-files", "0", + "total-position-deletes", "0", + "total-equality-deletes", "0"); + + List<Snapshot> snapshots = + Arrays.asList( + new BaseSnapshot( + 1L, 1L, null, 1234567890000L, "append", summary1, 1, "file:/tmp/manifest1.avro"), + new BaseSnapshot( + 2L, 2L, 1L, 9876543210000L, "append", summary2, 1, "file:/tmp/manifest2.avro")); + + return StaticDataTask.of( + Files.localInput("file:/tmp/metadata2.json"), + SnapshotsTable.SNAPSHOT_SCHEMA, + SnapshotsTable.SNAPSHOT_SCHEMA, + snapshots, + SnapshotsTable::snapshotToRow); + } + + private String snapshotsDataTaskJson() { + return "{\"task-type\":\"data-task\"," + + "\"schema\":{\"type\":\"struct\",\"schema-id\":0," + + "\"fields\":[{\"id\":1,\"name\":\"committed_at\",\"required\":true,\"type\":\"timestamptz\"}," + + "{\"id\":2,\"name\":\"snapshot_id\",\"required\":true,\"type\":\"long\"}," + + "{\"id\":3,\"name\":\"parent_id\",\"required\":false,\"type\":\"long\"}," + + "{\"id\":4,\"name\":\"operation\",\"required\":false,\"type\":\"string\"}," + + "{\"id\":5,\"name\":\"manifest_list\",\"required\":false,\"type\":\"string\"}," + + "{\"id\":6,\"name\":\"summary\",\"required\":false,\"type\":{\"type\":\"map\"," + + "\"key-id\":7,\"key\":\"string\",\"value-id\":8," + + "\"value\":\"string\",\"value-required\":true}}]}," + + "\"projection\":{\"type\":\"struct\",\"schema-id\":0," + + "\"fields\":[{\"id\":1,\"name\":\"committed_at\",\"required\":true,\"type\":\"timestamptz\"}," + + "{\"id\":2,\"name\":\"snapshot_id\",\"required\":true,\"type\":\"long\"}," + + "{\"id\":3,\"name\":\"parent_id\",\"required\":false,\"type\":\"long\"}," + + "{\"id\":4,\"name\":\"operation\",\"required\":false,\"type\":\"string\"}," + + "{\"id\":5,\"name\":\"manifest_list\",\"required\":false,\"type\":\"string\"}," + + "{\"id\":6,\"name\":\"summary\",\"required\":false,\"type\":{\"type\":\"map\"," + + "\"key-id\":7,\"key\":\"string\",\"value-id\":8," + + "\"value\":\"string\",\"value-required\":true}}]}," + + "\"metadata-file\":{\"spec-id\":0,\"content\":\"DATA\"," + + "\"file-path\":\"/tmp/metadata2.json\"," + + "\"file-format\":\"METADATA\",\"partition\":{}," + + "\"file-size-in-bytes\":0,\"record-count\":2,\"sort-order-id\":0}," + + "\"rows\":[{\"1\":\"2009-02-13T23:31:30+00:00\",\"2\":1,\"4\":\"append\"," + + "\"5\":\"file:/tmp/manifest1.avro\"," + + "\"6\":{\"keys\":[\"added-data-files\",\"added-records\",\"added-files-size\",\"changed-partition-count\"," + + "\"total-records\",\"total-files-size\",\"total-data-files\",\"total-delete-files\"," + + "\"total-position-deletes\",\"total-equality-deletes\"]," + + "\"values\":[\"1\",\"1\",\"10\",\"1\",\"1\",\"10\",\"1\",\"0\",\"0\",\"0\"]}}," + + "{\"1\":\"2282-12-22T20:13:30+00:00\",\"2\":2,\"3\":1,\"4\":\"append\"," + + "\"5\":\"file:/tmp/manifest2.avro\"," + + "\"6\":{\"keys\":[\"added-data-files\",\"added-records\",\"added-files-size\",\"changed-partition-count\"," + + "\"total-records\",\"total-files-size\",\"total-data-files\",\"total-delete-files\"," + + "\"total-position-deletes\",\"total-equality-deletes\"]," + + "\"values\":[\"1\",\"1\",\"10\",\"1\",\"2\",\"20\",\"2\",\"0\",\"0\",\"0\"]}}]}"; + } + + private void assertDataTaskEquals(StaticDataTask expected, StaticDataTask actual) { + assertThat(expected.schema().asStruct()) + .as("Schema should match") + .isEqualTo(actual.schema().asStruct()); + + assertThat(expected.projectedSchema().asStruct()) + .as("Projected schema should match") + .isEqualTo(actual.projectedSchema().asStruct()); + + TestContentFileParser.assertContentFileEquals( + expected.metadataFile(), actual.metadataFile(), PartitionSpec.unpartitioned()); + + List<StructLike> expectedRows = Lists.newArrayList(expected.rows()); + List<StructLike> actualRows = Lists.newArrayList(actual.rows()); + assertThat(actualRows).hasSize(expectedRows.size()); Review Comment: ```suggestion assertThat(actualRows).hasSameSizeAs(expectedRows); ``` -- 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-unsubscr...@iceberg.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org For additional commands, e-mail: issues-h...@iceberg.apache.org