nastra commented on code in PR #6934: URL: https://github.com/apache/iceberg/pull/6934#discussion_r1138547215
########## core/src/main/java/org/apache/iceberg/ContentFileParser.java: ########## @@ -0,0 +1,286 @@ +/* + * 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.nio.ByteBuffer; +import java.util.List; +import java.util.Map; +import org.apache.iceberg.relocated.com.google.common.base.Preconditions; +import org.apache.iceberg.util.ArrayUtil; +import org.apache.iceberg.util.JsonUtil; + +class ContentFileParser { + private static final String SPEC_ID = "spec-id"; + private static final String CONTENT = "content"; + private static final String FILE_PATH = "file-path"; + private static final String FILE_FORMAT = "file-format"; + private static final String PARTITION = "partition"; + private static final String RECORD_COUNT = "record-count"; + private static final String FILE_SIZE = "file-size-in-bytes"; + private static final String COLUMN_SIZES = "column-sizes"; + private static final String VALUE_COUNTS = "value-counts"; + private static final String NULL_VALUE_COUNTS = "null-value-counts"; + private static final String NAN_VALUE_COUNTS = "nan-value-counts"; + private static final String LOWER_BOUNDS = "lower-bounds"; + private static final String UPPER_BOUNDS = "upper-bounds"; + private static final String KEY_METADATA = "key-metadata"; + private static final String SPLIT_OFFSETS = "split-offsets"; + private static final String EQUALITY_IDS = "equality-ids"; + private static final String SORT_ORDER_ID = "sort-order-id"; + + private final PartitionSpec spec; + + ContentFileParser(PartitionSpec spec) { + this.spec = spec; + } + + private boolean hasPartitionData(StructLike partitionData) { + return partitionData != null && partitionData.size() > 0; + } + + void toJson(ContentFile contentFile, JsonGenerator generator) throws IOException { + generator.writeStartObject(); + + // ignore the ordinal position (ContentFile#pos) of the file in a manifest, + // as it isn't used and BaseFile constructor doesn't support it. + + Preconditions.checkArgument( + contentFile.specId() == spec.specId(), + "Partition spec id mismatch: expected = %s, actual = %s", + spec.specId(), + contentFile.specId()); + generator.writeNumberField(SPEC_ID, contentFile.specId()); + + generator.writeStringField(CONTENT, contentFile.content().name()); + generator.writeStringField(FILE_PATH, contentFile.path().toString()); + generator.writeStringField(FILE_FORMAT, contentFile.format().name()); + + Preconditions.checkArgument( + spec.isPartitioned() == hasPartitionData(contentFile.partition()), + "Invalid data file: partition data (%s) doesn't match the expected (%s)", + hasPartitionData(contentFile.partition()), + spec.isPartitioned()); + if (contentFile.partition() != null) { + generator.writeFieldName(PARTITION); + SingleValueParser.toJson(spec.partitionType(), contentFile.partition(), generator); + } + + generator.writeNumberField(RECORD_COUNT, contentFile.recordCount()); + generator.writeNumberField(FILE_SIZE, contentFile.fileSizeInBytes()); + + if (contentFile.columnSizes() != null) { + generator.writeFieldName(COLUMN_SIZES); + SingleValueParser.toJson(DataFile.COLUMN_SIZES.type(), contentFile.columnSizes(), generator); + } + + if (contentFile.valueCounts() != null) { + generator.writeFieldName(VALUE_COUNTS); + SingleValueParser.toJson(DataFile.VALUE_COUNTS.type(), contentFile.valueCounts(), generator); + } + + if (contentFile.nullValueCounts() != null) { + generator.writeFieldName(NULL_VALUE_COUNTS); + SingleValueParser.toJson( + DataFile.NULL_VALUE_COUNTS.type(), contentFile.nullValueCounts(), generator); + } + + if (contentFile.nullValueCounts() != null) { + generator.writeFieldName(NAN_VALUE_COUNTS); + SingleValueParser.toJson( + DataFile.NAN_VALUE_COUNTS.type(), contentFile.nanValueCounts(), generator); + } + + if (contentFile.lowerBounds() != null) { + generator.writeFieldName(LOWER_BOUNDS); + SingleValueParser.toJson(DataFile.LOWER_BOUNDS.type(), contentFile.lowerBounds(), generator); + } + + if (contentFile.upperBounds() != null) { + generator.writeFieldName(UPPER_BOUNDS); + SingleValueParser.toJson(DataFile.UPPER_BOUNDS.type(), contentFile.upperBounds(), generator); + } + + if (contentFile.keyMetadata() != null) { + generator.writeFieldName(KEY_METADATA); + SingleValueParser.toJson(DataFile.KEY_METADATA.type(), contentFile.keyMetadata(), generator); + } + + if (contentFile.splitOffsets() != null) { + generator.writeFieldName(SPLIT_OFFSETS); + SingleValueParser.toJson( + DataFile.SPLIT_OFFSETS.type(), contentFile.splitOffsets(), generator); + } + + if (contentFile.equalityFieldIds() != null) { + generator.writeFieldName(EQUALITY_IDS); + SingleValueParser.toJson( + DataFile.EQUALITY_IDS.type(), contentFile.equalityFieldIds(), generator); + } + + if (contentFile.sortOrderId() != null) { + generator.writeNumberField(SORT_ORDER_ID, contentFile.sortOrderId()); + } + + generator.writeEndObject(); + } + + ContentFile fromJson(JsonNode jsonNode) { + Preconditions.checkArgument( Review Comment: similarly here, we'd need to check for null: `Preconditions.checkArgument(null != jsonNode, "Cannot parse content file from null object");` ########## core/src/main/java/org/apache/iceberg/ContentFileParser.java: ########## @@ -0,0 +1,286 @@ +/* + * 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.nio.ByteBuffer; +import java.util.List; +import java.util.Map; +import org.apache.iceberg.relocated.com.google.common.base.Preconditions; +import org.apache.iceberg.util.ArrayUtil; +import org.apache.iceberg.util.JsonUtil; + +class ContentFileParser { + private static final String SPEC_ID = "spec-id"; + private static final String CONTENT = "content"; + private static final String FILE_PATH = "file-path"; + private static final String FILE_FORMAT = "file-format"; + private static final String PARTITION = "partition"; + private static final String RECORD_COUNT = "record-count"; + private static final String FILE_SIZE = "file-size-in-bytes"; + private static final String COLUMN_SIZES = "column-sizes"; + private static final String VALUE_COUNTS = "value-counts"; + private static final String NULL_VALUE_COUNTS = "null-value-counts"; + private static final String NAN_VALUE_COUNTS = "nan-value-counts"; + private static final String LOWER_BOUNDS = "lower-bounds"; + private static final String UPPER_BOUNDS = "upper-bounds"; + private static final String KEY_METADATA = "key-metadata"; + private static final String SPLIT_OFFSETS = "split-offsets"; + private static final String EQUALITY_IDS = "equality-ids"; + private static final String SORT_ORDER_ID = "sort-order-id"; + + private final PartitionSpec spec; + + ContentFileParser(PartitionSpec spec) { + this.spec = spec; + } + + private boolean hasPartitionData(StructLike partitionData) { + return partitionData != null && partitionData.size() > 0; + } + + void toJson(ContentFile contentFile, JsonGenerator generator) throws IOException { Review Comment: should this be `ContentFile<?>` rather than using raw types? ########## core/src/main/java/org/apache/iceberg/ContentFileParser.java: ########## @@ -0,0 +1,286 @@ +/* + * 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.nio.ByteBuffer; +import java.util.List; +import java.util.Map; +import org.apache.iceberg.relocated.com.google.common.base.Preconditions; +import org.apache.iceberg.util.ArrayUtil; +import org.apache.iceberg.util.JsonUtil; + +class ContentFileParser { + private static final String SPEC_ID = "spec-id"; + private static final String CONTENT = "content"; + private static final String FILE_PATH = "file-path"; + private static final String FILE_FORMAT = "file-format"; + private static final String PARTITION = "partition"; + private static final String RECORD_COUNT = "record-count"; + private static final String FILE_SIZE = "file-size-in-bytes"; + private static final String COLUMN_SIZES = "column-sizes"; + private static final String VALUE_COUNTS = "value-counts"; + private static final String NULL_VALUE_COUNTS = "null-value-counts"; + private static final String NAN_VALUE_COUNTS = "nan-value-counts"; + private static final String LOWER_BOUNDS = "lower-bounds"; + private static final String UPPER_BOUNDS = "upper-bounds"; + private static final String KEY_METADATA = "key-metadata"; + private static final String SPLIT_OFFSETS = "split-offsets"; + private static final String EQUALITY_IDS = "equality-ids"; + private static final String SORT_ORDER_ID = "sort-order-id"; + + private final PartitionSpec spec; + + ContentFileParser(PartitionSpec spec) { + this.spec = spec; + } + + private boolean hasPartitionData(StructLike partitionData) { + return partitionData != null && partitionData.size() > 0; + } + + void toJson(ContentFile contentFile, JsonGenerator generator) throws IOException { + generator.writeStartObject(); + + // ignore the ordinal position (ContentFile#pos) of the file in a manifest, + // as it isn't used and BaseFile constructor doesn't support it. + + Preconditions.checkArgument( + contentFile.specId() == spec.specId(), + "Partition spec id mismatch: expected = %s, actual = %s", + spec.specId(), + contentFile.specId()); + generator.writeNumberField(SPEC_ID, contentFile.specId()); + + generator.writeStringField(CONTENT, contentFile.content().name()); + generator.writeStringField(FILE_PATH, contentFile.path().toString()); + generator.writeStringField(FILE_FORMAT, contentFile.format().name()); + + Preconditions.checkArgument( + spec.isPartitioned() == hasPartitionData(contentFile.partition()), + "Invalid data file: partition data (%s) doesn't match the expected (%s)", + hasPartitionData(contentFile.partition()), + spec.isPartitioned()); + if (contentFile.partition() != null) { + generator.writeFieldName(PARTITION); + SingleValueParser.toJson(spec.partitionType(), contentFile.partition(), generator); + } + + generator.writeNumberField(RECORD_COUNT, contentFile.recordCount()); + generator.writeNumberField(FILE_SIZE, contentFile.fileSizeInBytes()); + + if (contentFile.columnSizes() != null) { + generator.writeFieldName(COLUMN_SIZES); + SingleValueParser.toJson(DataFile.COLUMN_SIZES.type(), contentFile.columnSizes(), generator); + } + + if (contentFile.valueCounts() != null) { + generator.writeFieldName(VALUE_COUNTS); + SingleValueParser.toJson(DataFile.VALUE_COUNTS.type(), contentFile.valueCounts(), generator); + } + + if (contentFile.nullValueCounts() != null) { + generator.writeFieldName(NULL_VALUE_COUNTS); + SingleValueParser.toJson( + DataFile.NULL_VALUE_COUNTS.type(), contentFile.nullValueCounts(), generator); + } + + if (contentFile.nullValueCounts() != null) { + generator.writeFieldName(NAN_VALUE_COUNTS); + SingleValueParser.toJson( + DataFile.NAN_VALUE_COUNTS.type(), contentFile.nanValueCounts(), generator); + } + + if (contentFile.lowerBounds() != null) { + generator.writeFieldName(LOWER_BOUNDS); + SingleValueParser.toJson(DataFile.LOWER_BOUNDS.type(), contentFile.lowerBounds(), generator); + } + + if (contentFile.upperBounds() != null) { + generator.writeFieldName(UPPER_BOUNDS); + SingleValueParser.toJson(DataFile.UPPER_BOUNDS.type(), contentFile.upperBounds(), generator); + } + + if (contentFile.keyMetadata() != null) { + generator.writeFieldName(KEY_METADATA); + SingleValueParser.toJson(DataFile.KEY_METADATA.type(), contentFile.keyMetadata(), generator); + } + + if (contentFile.splitOffsets() != null) { + generator.writeFieldName(SPLIT_OFFSETS); + SingleValueParser.toJson( + DataFile.SPLIT_OFFSETS.type(), contentFile.splitOffsets(), generator); + } + + if (contentFile.equalityFieldIds() != null) { + generator.writeFieldName(EQUALITY_IDS); + SingleValueParser.toJson( + DataFile.EQUALITY_IDS.type(), contentFile.equalityFieldIds(), generator); + } + + if (contentFile.sortOrderId() != null) { + generator.writeNumberField(SORT_ORDER_ID, contentFile.sortOrderId()); + } + + generator.writeEndObject(); + } + + ContentFile fromJson(JsonNode jsonNode) { + Preconditions.checkArgument( + jsonNode.isObject(), "Cannot parse content file from a non-object: %s", jsonNode); + + int specId = JsonUtil.getInt(SPEC_ID, jsonNode); + FileContent fileContent = FileContent.valueOf(JsonUtil.getString(CONTENT, jsonNode)); + String filePath = JsonUtil.getString(FILE_PATH, jsonNode); + FileFormat fileFormat = FileFormat.valueOf(JsonUtil.getString(FILE_FORMAT, jsonNode)); + + PartitionData partitionData = null; + if (jsonNode.has(PARTITION)) { + partitionData = new PartitionData(spec.partitionType()); + StructLike structLike = + (StructLike) SingleValueParser.fromJson(spec.partitionType(), jsonNode.get(PARTITION)); + Preconditions.checkState( + partitionData.size() == structLike.size(), + "Invalid partition data size: expected = %s, actual = %s", + partitionData.size(), + structLike.size()); + for (int pos = 0; pos < partitionData.size(); ++pos) { + Class<?> javaClass = spec.partitionType().fields().get(pos).type().typeId().javaClass(); + partitionData.set(pos, structLike.get(pos, javaClass)); + } + } + + long fileSizeInBytes = JsonUtil.getLong(FILE_SIZE, jsonNode); + Metrics metrics = toMetrics(jsonNode); + + ByteBuffer keyMetadata = null; + if (jsonNode.has(KEY_METADATA)) { + keyMetadata = + (ByteBuffer) + SingleValueParser.fromJson(DataFile.KEY_METADATA.type(), jsonNode.get(KEY_METADATA)); + } + + List<Long> splitOffsets = null; + if (jsonNode.has(SPLIT_OFFSETS)) { + splitOffsets = + (List<Long>) + SingleValueParser.fromJson( + DataFile.SPLIT_OFFSETS.type(), jsonNode.get(SPLIT_OFFSETS)); + } + + int[] equalityFieldIds = null; + if (jsonNode.has(EQUALITY_IDS)) { + equalityFieldIds = + ArrayUtil.toIntArray( + (List<Integer>) + SingleValueParser.fromJson( + DataFile.EQUALITY_IDS.type(), jsonNode.get(EQUALITY_IDS))); + } + + Integer sortOrderId = null; + if (jsonNode.has(SORT_ORDER_ID)) { + sortOrderId = JsonUtil.getInt(SORT_ORDER_ID, jsonNode); + } + + if (fileContent == FileContent.DATA) { + return new GenericDataFile( + specId, + filePath, + fileFormat, + partitionData, + fileSizeInBytes, + metrics, + keyMetadata, + splitOffsets, + equalityFieldIds, + sortOrderId); + } else { + return new GenericDeleteFile( + specId, + fileContent, + filePath, + fileFormat, + partitionData, + fileSizeInBytes, + metrics, + equalityFieldIds, + sortOrderId, + keyMetadata); + } + } + + private Metrics toMetrics(JsonNode jsonNode) { Review Comment: I think `metricsFromJson(..)` would be slightly clearer here ########## core/src/main/java/org/apache/iceberg/ContentFileParser.java: ########## @@ -0,0 +1,286 @@ +/* + * 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.nio.ByteBuffer; +import java.util.List; +import java.util.Map; +import org.apache.iceberg.relocated.com.google.common.base.Preconditions; +import org.apache.iceberg.util.ArrayUtil; +import org.apache.iceberg.util.JsonUtil; + +class ContentFileParser { + private static final String SPEC_ID = "spec-id"; + private static final String CONTENT = "content"; + private static final String FILE_PATH = "file-path"; + private static final String FILE_FORMAT = "file-format"; + private static final String PARTITION = "partition"; + private static final String RECORD_COUNT = "record-count"; + private static final String FILE_SIZE = "file-size-in-bytes"; + private static final String COLUMN_SIZES = "column-sizes"; + private static final String VALUE_COUNTS = "value-counts"; + private static final String NULL_VALUE_COUNTS = "null-value-counts"; + private static final String NAN_VALUE_COUNTS = "nan-value-counts"; + private static final String LOWER_BOUNDS = "lower-bounds"; + private static final String UPPER_BOUNDS = "upper-bounds"; + private static final String KEY_METADATA = "key-metadata"; + private static final String SPLIT_OFFSETS = "split-offsets"; + private static final String EQUALITY_IDS = "equality-ids"; + private static final String SORT_ORDER_ID = "sort-order-id"; + + private final PartitionSpec spec; + + ContentFileParser(PartitionSpec spec) { + this.spec = spec; + } + + private boolean hasPartitionData(StructLike partitionData) { + return partitionData != null && partitionData.size() > 0; + } + + void toJson(ContentFile contentFile, JsonGenerator generator) throws IOException { + generator.writeStartObject(); + + // ignore the ordinal position (ContentFile#pos) of the file in a manifest, + // as it isn't used and BaseFile constructor doesn't support it. + + Preconditions.checkArgument( + contentFile.specId() == spec.specId(), + "Partition spec id mismatch: expected = %s, actual = %s", + spec.specId(), + contentFile.specId()); + generator.writeNumberField(SPEC_ID, contentFile.specId()); + + generator.writeStringField(CONTENT, contentFile.content().name()); + generator.writeStringField(FILE_PATH, contentFile.path().toString()); + generator.writeStringField(FILE_FORMAT, contentFile.format().name()); + + Preconditions.checkArgument( + spec.isPartitioned() == hasPartitionData(contentFile.partition()), + "Invalid data file: partition data (%s) doesn't match the expected (%s)", + hasPartitionData(contentFile.partition()), + spec.isPartitioned()); + if (contentFile.partition() != null) { + generator.writeFieldName(PARTITION); + SingleValueParser.toJson(spec.partitionType(), contentFile.partition(), generator); + } + + generator.writeNumberField(RECORD_COUNT, contentFile.recordCount()); + generator.writeNumberField(FILE_SIZE, contentFile.fileSizeInBytes()); + + if (contentFile.columnSizes() != null) { + generator.writeFieldName(COLUMN_SIZES); + SingleValueParser.toJson(DataFile.COLUMN_SIZES.type(), contentFile.columnSizes(), generator); + } + + if (contentFile.valueCounts() != null) { + generator.writeFieldName(VALUE_COUNTS); + SingleValueParser.toJson(DataFile.VALUE_COUNTS.type(), contentFile.valueCounts(), generator); + } + + if (contentFile.nullValueCounts() != null) { + generator.writeFieldName(NULL_VALUE_COUNTS); + SingleValueParser.toJson( + DataFile.NULL_VALUE_COUNTS.type(), contentFile.nullValueCounts(), generator); + } + + if (contentFile.nullValueCounts() != null) { + generator.writeFieldName(NAN_VALUE_COUNTS); + SingleValueParser.toJson( + DataFile.NAN_VALUE_COUNTS.type(), contentFile.nanValueCounts(), generator); + } + + if (contentFile.lowerBounds() != null) { + generator.writeFieldName(LOWER_BOUNDS); + SingleValueParser.toJson(DataFile.LOWER_BOUNDS.type(), contentFile.lowerBounds(), generator); + } + + if (contentFile.upperBounds() != null) { + generator.writeFieldName(UPPER_BOUNDS); + SingleValueParser.toJson(DataFile.UPPER_BOUNDS.type(), contentFile.upperBounds(), generator); + } + + if (contentFile.keyMetadata() != null) { + generator.writeFieldName(KEY_METADATA); + SingleValueParser.toJson(DataFile.KEY_METADATA.type(), contentFile.keyMetadata(), generator); + } + + if (contentFile.splitOffsets() != null) { + generator.writeFieldName(SPLIT_OFFSETS); + SingleValueParser.toJson( + DataFile.SPLIT_OFFSETS.type(), contentFile.splitOffsets(), generator); + } + + if (contentFile.equalityFieldIds() != null) { + generator.writeFieldName(EQUALITY_IDS); + SingleValueParser.toJson( + DataFile.EQUALITY_IDS.type(), contentFile.equalityFieldIds(), generator); + } + + if (contentFile.sortOrderId() != null) { + generator.writeNumberField(SORT_ORDER_ID, contentFile.sortOrderId()); + } + + generator.writeEndObject(); + } + + ContentFile fromJson(JsonNode jsonNode) { + Preconditions.checkArgument( + jsonNode.isObject(), "Cannot parse content file from a non-object: %s", jsonNode); + + int specId = JsonUtil.getInt(SPEC_ID, jsonNode); + FileContent fileContent = FileContent.valueOf(JsonUtil.getString(CONTENT, jsonNode)); + String filePath = JsonUtil.getString(FILE_PATH, jsonNode); + FileFormat fileFormat = FileFormat.valueOf(JsonUtil.getString(FILE_FORMAT, jsonNode)); Review Comment: probably good to use `FileFormat.fromString(..)` here as it has better error reporting if it can't parse a string ########## core/src/test/java/org/apache/iceberg/TableTestBase.java: ########## @@ -57,7 +57,7 @@ public class TableTestBase { protected static final int BUCKETS_NUMBER = 16; // Partition spec used to create tables - protected static final PartitionSpec SPEC = + public static final PartitionSpec SPEC = Review Comment: maybe worth having a separate spec in the tests rather than making this one public and using it, wdyt? ########## core/src/main/java/org/apache/iceberg/ContentFileParser.java: ########## @@ -0,0 +1,286 @@ +/* + * 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.nio.ByteBuffer; +import java.util.List; +import java.util.Map; +import org.apache.iceberg.relocated.com.google.common.base.Preconditions; +import org.apache.iceberg.util.ArrayUtil; +import org.apache.iceberg.util.JsonUtil; + +class ContentFileParser { + private static final String SPEC_ID = "spec-id"; + private static final String CONTENT = "content"; + private static final String FILE_PATH = "file-path"; + private static final String FILE_FORMAT = "file-format"; + private static final String PARTITION = "partition"; + private static final String RECORD_COUNT = "record-count"; + private static final String FILE_SIZE = "file-size-in-bytes"; + private static final String COLUMN_SIZES = "column-sizes"; + private static final String VALUE_COUNTS = "value-counts"; + private static final String NULL_VALUE_COUNTS = "null-value-counts"; + private static final String NAN_VALUE_COUNTS = "nan-value-counts"; + private static final String LOWER_BOUNDS = "lower-bounds"; + private static final String UPPER_BOUNDS = "upper-bounds"; + private static final String KEY_METADATA = "key-metadata"; + private static final String SPLIT_OFFSETS = "split-offsets"; + private static final String EQUALITY_IDS = "equality-ids"; + private static final String SORT_ORDER_ID = "sort-order-id"; + + private final PartitionSpec spec; + + ContentFileParser(PartitionSpec spec) { + this.spec = spec; + } + + private boolean hasPartitionData(StructLike partitionData) { + return partitionData != null && partitionData.size() > 0; + } + + void toJson(ContentFile contentFile, JsonGenerator generator) throws IOException { + generator.writeStartObject(); + + // ignore the ordinal position (ContentFile#pos) of the file in a manifest, + // as it isn't used and BaseFile constructor doesn't support it. + + Preconditions.checkArgument( + contentFile.specId() == spec.specId(), + "Partition spec id mismatch: expected = %s, actual = %s", + spec.specId(), + contentFile.specId()); + generator.writeNumberField(SPEC_ID, contentFile.specId()); + + generator.writeStringField(CONTENT, contentFile.content().name()); + generator.writeStringField(FILE_PATH, contentFile.path().toString()); + generator.writeStringField(FILE_FORMAT, contentFile.format().name()); + + Preconditions.checkArgument( + spec.isPartitioned() == hasPartitionData(contentFile.partition()), + "Invalid data file: partition data (%s) doesn't match the expected (%s)", + hasPartitionData(contentFile.partition()), + spec.isPartitioned()); + if (contentFile.partition() != null) { + generator.writeFieldName(PARTITION); + SingleValueParser.toJson(spec.partitionType(), contentFile.partition(), generator); + } + + generator.writeNumberField(RECORD_COUNT, contentFile.recordCount()); + generator.writeNumberField(FILE_SIZE, contentFile.fileSizeInBytes()); + + if (contentFile.columnSizes() != null) { + generator.writeFieldName(COLUMN_SIZES); + SingleValueParser.toJson(DataFile.COLUMN_SIZES.type(), contentFile.columnSizes(), generator); + } + + if (contentFile.valueCounts() != null) { + generator.writeFieldName(VALUE_COUNTS); + SingleValueParser.toJson(DataFile.VALUE_COUNTS.type(), contentFile.valueCounts(), generator); + } + + if (contentFile.nullValueCounts() != null) { + generator.writeFieldName(NULL_VALUE_COUNTS); + SingleValueParser.toJson( + DataFile.NULL_VALUE_COUNTS.type(), contentFile.nullValueCounts(), generator); + } + + if (contentFile.nullValueCounts() != null) { + generator.writeFieldName(NAN_VALUE_COUNTS); + SingleValueParser.toJson( + DataFile.NAN_VALUE_COUNTS.type(), contentFile.nanValueCounts(), generator); + } + + if (contentFile.lowerBounds() != null) { + generator.writeFieldName(LOWER_BOUNDS); + SingleValueParser.toJson(DataFile.LOWER_BOUNDS.type(), contentFile.lowerBounds(), generator); + } + + if (contentFile.upperBounds() != null) { + generator.writeFieldName(UPPER_BOUNDS); + SingleValueParser.toJson(DataFile.UPPER_BOUNDS.type(), contentFile.upperBounds(), generator); + } + + if (contentFile.keyMetadata() != null) { + generator.writeFieldName(KEY_METADATA); + SingleValueParser.toJson(DataFile.KEY_METADATA.type(), contentFile.keyMetadata(), generator); + } + + if (contentFile.splitOffsets() != null) { + generator.writeFieldName(SPLIT_OFFSETS); + SingleValueParser.toJson( + DataFile.SPLIT_OFFSETS.type(), contentFile.splitOffsets(), generator); + } + + if (contentFile.equalityFieldIds() != null) { + generator.writeFieldName(EQUALITY_IDS); + SingleValueParser.toJson( + DataFile.EQUALITY_IDS.type(), contentFile.equalityFieldIds(), generator); + } + + if (contentFile.sortOrderId() != null) { + generator.writeNumberField(SORT_ORDER_ID, contentFile.sortOrderId()); + } + + generator.writeEndObject(); + } + + ContentFile fromJson(JsonNode jsonNode) { + Preconditions.checkArgument( + jsonNode.isObject(), "Cannot parse content file from a non-object: %s", jsonNode); + + int specId = JsonUtil.getInt(SPEC_ID, jsonNode); + FileContent fileContent = FileContent.valueOf(JsonUtil.getString(CONTENT, jsonNode)); + String filePath = JsonUtil.getString(FILE_PATH, jsonNode); + FileFormat fileFormat = FileFormat.valueOf(JsonUtil.getString(FILE_FORMAT, jsonNode)); + + PartitionData partitionData = null; + if (jsonNode.has(PARTITION)) { + partitionData = new PartitionData(spec.partitionType()); + StructLike structLike = + (StructLike) SingleValueParser.fromJson(spec.partitionType(), jsonNode.get(PARTITION)); + Preconditions.checkState( + partitionData.size() == structLike.size(), + "Invalid partition data size: expected = %s, actual = %s", + partitionData.size(), + structLike.size()); + for (int pos = 0; pos < partitionData.size(); ++pos) { + Class<?> javaClass = spec.partitionType().fields().get(pos).type().typeId().javaClass(); + partitionData.set(pos, structLike.get(pos, javaClass)); + } + } + + long fileSizeInBytes = JsonUtil.getLong(FILE_SIZE, jsonNode); + Metrics metrics = toMetrics(jsonNode); + + ByteBuffer keyMetadata = null; + if (jsonNode.has(KEY_METADATA)) { + keyMetadata = + (ByteBuffer) + SingleValueParser.fromJson(DataFile.KEY_METADATA.type(), jsonNode.get(KEY_METADATA)); + } + + List<Long> splitOffsets = null; + if (jsonNode.has(SPLIT_OFFSETS)) { + splitOffsets = + (List<Long>) + SingleValueParser.fromJson( + DataFile.SPLIT_OFFSETS.type(), jsonNode.get(SPLIT_OFFSETS)); + } + + int[] equalityFieldIds = null; + if (jsonNode.has(EQUALITY_IDS)) { + equalityFieldIds = + ArrayUtil.toIntArray( + (List<Integer>) + SingleValueParser.fromJson( + DataFile.EQUALITY_IDS.type(), jsonNode.get(EQUALITY_IDS))); + } + + Integer sortOrderId = null; + if (jsonNode.has(SORT_ORDER_ID)) { + sortOrderId = JsonUtil.getInt(SORT_ORDER_ID, jsonNode); + } + + if (fileContent == FileContent.DATA) { + return new GenericDataFile( + specId, + filePath, + fileFormat, + partitionData, + fileSizeInBytes, + metrics, + keyMetadata, + splitOffsets, + equalityFieldIds, + sortOrderId); + } else { + return new GenericDeleteFile( + specId, + fileContent, + filePath, + fileFormat, + partitionData, + fileSizeInBytes, + metrics, + equalityFieldIds, + sortOrderId, + keyMetadata); + } + } + + private Metrics toMetrics(JsonNode jsonNode) { + long recordCount = JsonUtil.getLong(RECORD_COUNT, jsonNode); + + Map<Integer, Long> columnSizes = null; + if (jsonNode.has(COLUMN_SIZES)) { + columnSizes = + (Map<Integer, Long>) + SingleValueParser.fromJson(DataFile.COLUMN_SIZES.type(), jsonNode.get(COLUMN_SIZES)); Review Comment: We generally try and use `JsonUtil.get(xyz, jsonNode)` rather than `jsonNode.get(xyz)` so that we have better error reporting when we can't find a field (but I guess here it doesn't matter because we checked the field already for existence) ########## core/src/main/java/org/apache/iceberg/FileScanTaskParser.java: ########## @@ -0,0 +1,141 @@ +/* + * 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 com.github.benmanes.caffeine.cache.Caffeine; +import com.github.benmanes.caffeine.cache.LoadingCache; +import java.io.IOException; +import java.io.StringWriter; +import java.io.UncheckedIOException; +import org.apache.iceberg.expressions.Expression; +import org.apache.iceberg.expressions.ExpressionParser; +import org.apache.iceberg.expressions.Expressions; +import org.apache.iceberg.expressions.ResidualEvaluator; +import org.apache.iceberg.relocated.com.google.common.base.Preconditions; +import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList; +import org.apache.iceberg.util.JsonUtil; + +public class FileScanTaskParser { + private static final String SCHEMA = "schema"; + private static final String SPEC = "spec"; + private static final String DATA_FILE = "data-file"; + private static final String DELETE_FILES = "delete-files"; + private static final String RESIDUAL = "residual-filter"; + + private final boolean caseSensitive; + private final LoadingCache<PartitionSpec, ContentFileParser> contentFileParsersBySpec; + + public FileScanTaskParser(boolean caseSensitive) { + this.caseSensitive = caseSensitive; + this.contentFileParsersBySpec = + Caffeine.newBuilder().weakKeys().build(spec -> new ContentFileParser(spec)); + } + + public String toJson(FileScanTask fileScanTask) { + try (StringWriter writer = new StringWriter()) { + JsonGenerator generator = JsonUtil.factory().createGenerator(writer); + toJson(fileScanTask, generator); + generator.flush(); + return writer.toString(); + } catch (IOException e) { + throw new UncheckedIOException("Failed to write json for: " + fileScanTask, e); + } + } + + void toJson(FileScanTask fileScanTask, JsonGenerator generator) throws IOException { + generator.writeStartObject(); + + generator.writeFieldName(SCHEMA); + SchemaParser.toJson(fileScanTask.schema(), generator); + + generator.writeFieldName(SPEC); + PartitionSpecParser.toJson(fileScanTask.spec(), generator); + + ContentFileParser contentFileParser = contentFileParsersBySpec.get(fileScanTask.spec()); Review Comment: instead of using a cache, what if `ContentFileParser` would be completely static and we would just pass the spec to the respective toJson/fromJson methods? ########## core/src/main/java/org/apache/iceberg/FileScanTaskParser.java: ########## @@ -0,0 +1,141 @@ +/* + * 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 com.github.benmanes.caffeine.cache.Caffeine; +import com.github.benmanes.caffeine.cache.LoadingCache; +import java.io.IOException; +import java.io.StringWriter; +import java.io.UncheckedIOException; +import org.apache.iceberg.expressions.Expression; +import org.apache.iceberg.expressions.ExpressionParser; +import org.apache.iceberg.expressions.Expressions; +import org.apache.iceberg.expressions.ResidualEvaluator; +import org.apache.iceberg.relocated.com.google.common.base.Preconditions; +import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList; +import org.apache.iceberg.util.JsonUtil; + +public class FileScanTaskParser { + private static final String SCHEMA = "schema"; + private static final String SPEC = "spec"; + private static final String DATA_FILE = "data-file"; + private static final String DELETE_FILES = "delete-files"; + private static final String RESIDUAL = "residual-filter"; + + private final boolean caseSensitive; + private final LoadingCache<PartitionSpec, ContentFileParser> contentFileParsersBySpec; + + public FileScanTaskParser(boolean caseSensitive) { + this.caseSensitive = caseSensitive; + this.contentFileParsersBySpec = + Caffeine.newBuilder().weakKeys().build(spec -> new ContentFileParser(spec)); + } + + public String toJson(FileScanTask fileScanTask) { + try (StringWriter writer = new StringWriter()) { + JsonGenerator generator = JsonUtil.factory().createGenerator(writer); + toJson(fileScanTask, generator); + generator.flush(); + return writer.toString(); + } catch (IOException e) { + throw new UncheckedIOException("Failed to write json for: " + fileScanTask, e); + } + } + + void toJson(FileScanTask fileScanTask, JsonGenerator generator) throws IOException { + generator.writeStartObject(); Review Comment: same here as in the other class, we'd need to add a null check + tests ########## core/src/main/java/org/apache/iceberg/FileScanTaskParser.java: ########## @@ -0,0 +1,141 @@ +/* + * 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 com.github.benmanes.caffeine.cache.Caffeine; +import com.github.benmanes.caffeine.cache.LoadingCache; +import java.io.IOException; +import java.io.StringWriter; +import java.io.UncheckedIOException; +import org.apache.iceberg.expressions.Expression; +import org.apache.iceberg.expressions.ExpressionParser; +import org.apache.iceberg.expressions.Expressions; +import org.apache.iceberg.expressions.ResidualEvaluator; +import org.apache.iceberg.relocated.com.google.common.base.Preconditions; +import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList; +import org.apache.iceberg.util.JsonUtil; + +public class FileScanTaskParser { + private static final String SCHEMA = "schema"; + private static final String SPEC = "spec"; + private static final String DATA_FILE = "data-file"; + private static final String DELETE_FILES = "delete-files"; + private static final String RESIDUAL = "residual-filter"; + + private final boolean caseSensitive; + private final LoadingCache<PartitionSpec, ContentFileParser> contentFileParsersBySpec; + + public FileScanTaskParser(boolean caseSensitive) { + this.caseSensitive = caseSensitive; + this.contentFileParsersBySpec = + Caffeine.newBuilder().weakKeys().build(spec -> new ContentFileParser(spec)); + } + + public String toJson(FileScanTask fileScanTask) { + try (StringWriter writer = new StringWriter()) { + JsonGenerator generator = JsonUtil.factory().createGenerator(writer); + toJson(fileScanTask, generator); + generator.flush(); + return writer.toString(); + } catch (IOException e) { + throw new UncheckedIOException("Failed to write json for: " + fileScanTask, e); + } + } + + void toJson(FileScanTask fileScanTask, JsonGenerator generator) throws IOException { + generator.writeStartObject(); + + generator.writeFieldName(SCHEMA); + SchemaParser.toJson(fileScanTask.schema(), generator); + + generator.writeFieldName(SPEC); + PartitionSpecParser.toJson(fileScanTask.spec(), generator); + + ContentFileParser contentFileParser = contentFileParsersBySpec.get(fileScanTask.spec()); + + if (fileScanTask.file() != null) { + generator.writeFieldName(DATA_FILE); + contentFileParser.toJson(fileScanTask.file(), generator); + } + + if (fileScanTask.deletes() != null) { + generator.writeArrayFieldStart(DELETE_FILES); + for (DeleteFile deleteFile : fileScanTask.deletes()) { + contentFileParser.toJson(deleteFile, generator); + } + generator.writeEndArray(); + } + + if (fileScanTask.residual() != null) { + generator.writeFieldName(RESIDUAL); + ExpressionParser.toJson(fileScanTask.residual(), generator); + } + + generator.writeEndObject(); + } + + public FileScanTask fromJson(String json) { + return JsonUtil.parse(json, this::fromJson); + } + + FileScanTask fromJson(JsonNode jsonNode) { + Preconditions.checkArgument( Review Comment: same here wrt null checking + tests. ########## core/src/main/java/org/apache/iceberg/ContentFileParser.java: ########## @@ -0,0 +1,286 @@ +/* + * 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.nio.ByteBuffer; +import java.util.List; +import java.util.Map; +import org.apache.iceberg.relocated.com.google.common.base.Preconditions; +import org.apache.iceberg.util.ArrayUtil; +import org.apache.iceberg.util.JsonUtil; + +class ContentFileParser { + private static final String SPEC_ID = "spec-id"; + private static final String CONTENT = "content"; + private static final String FILE_PATH = "file-path"; + private static final String FILE_FORMAT = "file-format"; + private static final String PARTITION = "partition"; + private static final String RECORD_COUNT = "record-count"; + private static final String FILE_SIZE = "file-size-in-bytes"; + private static final String COLUMN_SIZES = "column-sizes"; + private static final String VALUE_COUNTS = "value-counts"; + private static final String NULL_VALUE_COUNTS = "null-value-counts"; + private static final String NAN_VALUE_COUNTS = "nan-value-counts"; + private static final String LOWER_BOUNDS = "lower-bounds"; + private static final String UPPER_BOUNDS = "upper-bounds"; + private static final String KEY_METADATA = "key-metadata"; + private static final String SPLIT_OFFSETS = "split-offsets"; + private static final String EQUALITY_IDS = "equality-ids"; + private static final String SORT_ORDER_ID = "sort-order-id"; + + private final PartitionSpec spec; + + ContentFileParser(PartitionSpec spec) { + this.spec = spec; + } + + private boolean hasPartitionData(StructLike partitionData) { + return partitionData != null && partitionData.size() > 0; + } + + void toJson(ContentFile contentFile, JsonGenerator generator) throws IOException { Review Comment: we would need to do a null check on `contentFile` + have a unit test for it, similar to how we do it in other parsers such as `CounterResultParser` + `TestCounterResultParser` ########## core/src/main/java/org/apache/iceberg/FileScanTaskParser.java: ########## @@ -0,0 +1,141 @@ +/* + * 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 com.github.benmanes.caffeine.cache.Caffeine; +import com.github.benmanes.caffeine.cache.LoadingCache; +import java.io.IOException; +import java.io.StringWriter; +import java.io.UncheckedIOException; +import org.apache.iceberg.expressions.Expression; +import org.apache.iceberg.expressions.ExpressionParser; +import org.apache.iceberg.expressions.Expressions; +import org.apache.iceberg.expressions.ResidualEvaluator; +import org.apache.iceberg.relocated.com.google.common.base.Preconditions; +import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList; +import org.apache.iceberg.util.JsonUtil; + +public class FileScanTaskParser { + private static final String SCHEMA = "schema"; + private static final String SPEC = "spec"; + private static final String DATA_FILE = "data-file"; + private static final String DELETE_FILES = "delete-files"; + private static final String RESIDUAL = "residual-filter"; + + private final boolean caseSensitive; + private final LoadingCache<PartitionSpec, ContentFileParser> contentFileParsersBySpec; + + public FileScanTaskParser(boolean caseSensitive) { Review Comment: what if `caseSensitive` would be part of of the fromJson/toJson methods? Then the `toJson` in L52 could just re-use functionality from `JsonUtil.generate()` (similar to how we do it in https://github.com/apache/iceberg/blob/2e45760c8d2b62cc46584cbff28bcb2df422d7ff/core/src/main/java/org/apache/iceberg/metrics/CounterResultParser.java#L38-L44) ########## core/src/main/java/org/apache/iceberg/ContentFileParser.java: ########## @@ -0,0 +1,286 @@ +/* + * 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.nio.ByteBuffer; +import java.util.List; +import java.util.Map; +import org.apache.iceberg.relocated.com.google.common.base.Preconditions; +import org.apache.iceberg.util.ArrayUtil; +import org.apache.iceberg.util.JsonUtil; + +class ContentFileParser { + private static final String SPEC_ID = "spec-id"; + private static final String CONTENT = "content"; + private static final String FILE_PATH = "file-path"; + private static final String FILE_FORMAT = "file-format"; + private static final String PARTITION = "partition"; + private static final String RECORD_COUNT = "record-count"; + private static final String FILE_SIZE = "file-size-in-bytes"; + private static final String COLUMN_SIZES = "column-sizes"; + private static final String VALUE_COUNTS = "value-counts"; + private static final String NULL_VALUE_COUNTS = "null-value-counts"; + private static final String NAN_VALUE_COUNTS = "nan-value-counts"; + private static final String LOWER_BOUNDS = "lower-bounds"; + private static final String UPPER_BOUNDS = "upper-bounds"; + private static final String KEY_METADATA = "key-metadata"; + private static final String SPLIT_OFFSETS = "split-offsets"; + private static final String EQUALITY_IDS = "equality-ids"; + private static final String SORT_ORDER_ID = "sort-order-id"; + + private final PartitionSpec spec; + + ContentFileParser(PartitionSpec spec) { + this.spec = spec; + } + + private boolean hasPartitionData(StructLike partitionData) { + return partitionData != null && partitionData.size() > 0; + } + + void toJson(ContentFile contentFile, JsonGenerator generator) throws IOException { + generator.writeStartObject(); + + // ignore the ordinal position (ContentFile#pos) of the file in a manifest, + // as it isn't used and BaseFile constructor doesn't support it. + + Preconditions.checkArgument( + contentFile.specId() == spec.specId(), + "Partition spec id mismatch: expected = %s, actual = %s", + spec.specId(), + contentFile.specId()); + generator.writeNumberField(SPEC_ID, contentFile.specId()); + + generator.writeStringField(CONTENT, contentFile.content().name()); + generator.writeStringField(FILE_PATH, contentFile.path().toString()); + generator.writeStringField(FILE_FORMAT, contentFile.format().name()); + + Preconditions.checkArgument( + spec.isPartitioned() == hasPartitionData(contentFile.partition()), + "Invalid data file: partition data (%s) doesn't match the expected (%s)", + hasPartitionData(contentFile.partition()), + spec.isPartitioned()); + if (contentFile.partition() != null) { + generator.writeFieldName(PARTITION); + SingleValueParser.toJson(spec.partitionType(), contentFile.partition(), generator); + } + + generator.writeNumberField(RECORD_COUNT, contentFile.recordCount()); + generator.writeNumberField(FILE_SIZE, contentFile.fileSizeInBytes()); + + if (contentFile.columnSizes() != null) { + generator.writeFieldName(COLUMN_SIZES); + SingleValueParser.toJson(DataFile.COLUMN_SIZES.type(), contentFile.columnSizes(), generator); + } + + if (contentFile.valueCounts() != null) { + generator.writeFieldName(VALUE_COUNTS); + SingleValueParser.toJson(DataFile.VALUE_COUNTS.type(), contentFile.valueCounts(), generator); + } + + if (contentFile.nullValueCounts() != null) { + generator.writeFieldName(NULL_VALUE_COUNTS); + SingleValueParser.toJson( + DataFile.NULL_VALUE_COUNTS.type(), contentFile.nullValueCounts(), generator); + } + + if (contentFile.nullValueCounts() != null) { + generator.writeFieldName(NAN_VALUE_COUNTS); + SingleValueParser.toJson( + DataFile.NAN_VALUE_COUNTS.type(), contentFile.nanValueCounts(), generator); + } + + if (contentFile.lowerBounds() != null) { + generator.writeFieldName(LOWER_BOUNDS); + SingleValueParser.toJson(DataFile.LOWER_BOUNDS.type(), contentFile.lowerBounds(), generator); + } + + if (contentFile.upperBounds() != null) { + generator.writeFieldName(UPPER_BOUNDS); + SingleValueParser.toJson(DataFile.UPPER_BOUNDS.type(), contentFile.upperBounds(), generator); + } + + if (contentFile.keyMetadata() != null) { + generator.writeFieldName(KEY_METADATA); + SingleValueParser.toJson(DataFile.KEY_METADATA.type(), contentFile.keyMetadata(), generator); + } + + if (contentFile.splitOffsets() != null) { + generator.writeFieldName(SPLIT_OFFSETS); + SingleValueParser.toJson( + DataFile.SPLIT_OFFSETS.type(), contentFile.splitOffsets(), generator); + } + + if (contentFile.equalityFieldIds() != null) { + generator.writeFieldName(EQUALITY_IDS); + SingleValueParser.toJson( + DataFile.EQUALITY_IDS.type(), contentFile.equalityFieldIds(), generator); + } + + if (contentFile.sortOrderId() != null) { + generator.writeNumberField(SORT_ORDER_ID, contentFile.sortOrderId()); + } + + generator.writeEndObject(); + } + + ContentFile fromJson(JsonNode jsonNode) { + Preconditions.checkArgument( + jsonNode.isObject(), "Cannot parse content file from a non-object: %s", jsonNode); + + int specId = JsonUtil.getInt(SPEC_ID, jsonNode); + FileContent fileContent = FileContent.valueOf(JsonUtil.getString(CONTENT, jsonNode)); + String filePath = JsonUtil.getString(FILE_PATH, jsonNode); + FileFormat fileFormat = FileFormat.valueOf(JsonUtil.getString(FILE_FORMAT, jsonNode)); + + PartitionData partitionData = null; + if (jsonNode.has(PARTITION)) { + partitionData = new PartitionData(spec.partitionType()); + StructLike structLike = + (StructLike) SingleValueParser.fromJson(spec.partitionType(), jsonNode.get(PARTITION)); + Preconditions.checkState( + partitionData.size() == structLike.size(), + "Invalid partition data size: expected = %s, actual = %s", + partitionData.size(), + structLike.size()); + for (int pos = 0; pos < partitionData.size(); ++pos) { + Class<?> javaClass = spec.partitionType().fields().get(pos).type().typeId().javaClass(); + partitionData.set(pos, structLike.get(pos, javaClass)); + } + } + + long fileSizeInBytes = JsonUtil.getLong(FILE_SIZE, jsonNode); + Metrics metrics = toMetrics(jsonNode); + + ByteBuffer keyMetadata = null; + if (jsonNode.has(KEY_METADATA)) { + keyMetadata = + (ByteBuffer) + SingleValueParser.fromJson(DataFile.KEY_METADATA.type(), jsonNode.get(KEY_METADATA)); + } + + List<Long> splitOffsets = null; + if (jsonNode.has(SPLIT_OFFSETS)) { + splitOffsets = + (List<Long>) + SingleValueParser.fromJson( + DataFile.SPLIT_OFFSETS.type(), jsonNode.get(SPLIT_OFFSETS)); + } + + int[] equalityFieldIds = null; + if (jsonNode.has(EQUALITY_IDS)) { + equalityFieldIds = + ArrayUtil.toIntArray( + (List<Integer>) + SingleValueParser.fromJson( + DataFile.EQUALITY_IDS.type(), jsonNode.get(EQUALITY_IDS))); + } + + Integer sortOrderId = null; + if (jsonNode.has(SORT_ORDER_ID)) { + sortOrderId = JsonUtil.getInt(SORT_ORDER_ID, jsonNode); + } + + if (fileContent == FileContent.DATA) { + return new GenericDataFile( + specId, + filePath, + fileFormat, + partitionData, + fileSizeInBytes, + metrics, + keyMetadata, + splitOffsets, + equalityFieldIds, + sortOrderId); + } else { + return new GenericDeleteFile( + specId, + fileContent, + filePath, + fileFormat, + partitionData, + fileSizeInBytes, + metrics, + equalityFieldIds, + sortOrderId, + keyMetadata); + } + } + + private Metrics toMetrics(JsonNode jsonNode) { + long recordCount = JsonUtil.getLong(RECORD_COUNT, jsonNode); + + Map<Integer, Long> columnSizes = null; + if (jsonNode.has(COLUMN_SIZES)) { + columnSizes = + (Map<Integer, Long>) + SingleValueParser.fromJson(DataFile.COLUMN_SIZES.type(), jsonNode.get(COLUMN_SIZES)); + } + + Map<Integer, Long> valueCounts = null; + if (jsonNode.has(VALUE_COUNTS)) { + valueCounts = + (Map<Integer, Long>) + SingleValueParser.fromJson(DataFile.VALUE_COUNTS.type(), jsonNode.get(VALUE_COUNTS)); + } + + Map<Integer, Long> nullValueCounts = null; + if (jsonNode.has(NULL_VALUE_COUNTS)) { + nullValueCounts = + (Map<Integer, Long>) Review Comment: with all the casts I wonder if we should add an additional check to make sure the result is in fact an instance of what we're expecting -- 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]
