Skip to content

Commit

Permalink
GH-3086: Allow for empty beans (#3087)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fokko committed Nov 28, 2024
1 parent c120d8f commit cc782d9
Show file tree
Hide file tree
Showing 4 changed files with 620 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1591,7 +1591,9 @@ public FileMetaDataAndRowGroupOffsetInfo visit(RangeMetadataFilter filter) throw

ParquetMetadata parquetMetadata =
fromParquetMetadata(fileMetaData, fileDecryptor, encryptedFooter, rowGroupToRowIndexOffsetMap);
if (LOG.isDebugEnabled()) LOG.debug(ParquetMetadata.toPrettyJSON(parquetMetadata));
if (LOG.isDebugEnabled()) {
LOG.debug(ParquetMetadata.toPrettyJSON(parquetMetadata));
}
return parquetMetadata;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,20 @@

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.databind.SerializationFeature;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.List;

/**
* Metadata block stored in the footer of the file
* contains file level (Codec, Schema, ...) and block level (location, columns, record count, ...) meta data
* contains file level (Codec, Schema, ...) and block level (location, columns, record count, ...) metadata
*/
public class ParquetMetadata {

private static final ObjectMapper objectMapper = new ObjectMapper();
private static final ObjectMapper objectMapper =
new ObjectMapper().configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);

/**
* @param parquetMetaData an instance of parquet metadata to convert
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* 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.parquet.hadoop.metadata;

import static org.apache.parquet.schema.LogicalTypeAnnotation.TimeUnit.MICROS;
import static org.apache.parquet.schema.LogicalTypeAnnotation.TimeUnit.MILLIS;
import static org.apache.parquet.schema.LogicalTypeAnnotation.TimeUnit.NANOS;
import static org.apache.parquet.schema.PrimitiveType.PrimitiveTypeName.BINARY;
import static org.apache.parquet.schema.PrimitiveType.PrimitiveTypeName.FIXED_LEN_BYTE_ARRAY;
import static org.apache.parquet.schema.PrimitiveType.PrimitiveTypeName.FLOAT;
import static org.apache.parquet.schema.PrimitiveType.PrimitiveTypeName.INT32;
import static org.apache.parquet.schema.PrimitiveType.PrimitiveTypeName.INT64;
import static org.junit.Assert.assertEquals;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Collections;
import org.apache.parquet.schema.LogicalTypeAnnotation;
import org.apache.parquet.schema.MessageType;
import org.apache.parquet.schema.Types;
import org.junit.Test;

public class TestParquetMetadata {
private static final String EXPECTED_JSON = "/test-expected-parquet-metadata.json";

// Use an object mapper, since order of the keys might differ in JSON maps,
// and we don't want to sort them because of testing
private static final ObjectMapper mapper = new ObjectMapper();

private static JsonNode expectedJson() throws IOException, URISyntaxException {
URI path = TestParquetMetadata.class.getResource(EXPECTED_JSON).toURI();
return mapper.readTree(path.toURL());
}

@Test
public void testToPrettyJSON() throws IOException, URISyntaxException {
MessageType complexParquetSchema = Types.buildMessage()
.addField(Types.optional(INT32)
.as(LogicalTypeAnnotation.intType(8))
.named("a"))
.addField(Types.optionalGroup()
.addField(Types.optional(INT32)
.as(LogicalTypeAnnotation.intType(16))
.named("c"))
.addField(Types.optional(BINARY)
.as(LogicalTypeAnnotation.stringType())
.named("d"))
.named("b"))
.addField(Types.optionalList()
.setElementType(Types.optional(INT32)
.as(LogicalTypeAnnotation.dateType())
.named("element"))
.named("e"))
.addField(Types.optionalList()
.setElementType(Types.optional(INT32)
.as(LogicalTypeAnnotation.dateType())
.named("element"))
.named("f"))
.addField(Types.optional(FLOAT).named("g"))
.addField(Types.optional(INT64)
.as(LogicalTypeAnnotation.timestampType(true, MILLIS))
.named("h"))
.addField(Types.optional(INT64)
.as(LogicalTypeAnnotation.timestampType(true, NANOS))
.named("i"))
.addField(Types.optional(INT64)
.as(LogicalTypeAnnotation.timestampType(false, MILLIS))
.named("j"))
.addField(Types.optional(INT64)
.as(LogicalTypeAnnotation.timestampType(true, MICROS))
.named("k"))
.addField(Types.optional(INT64)
.as(LogicalTypeAnnotation.timestampType(false, MICROS))
.named("l"))
.addField(Types.optional(FIXED_LEN_BYTE_ARRAY)
.length(12)
.as(LogicalTypeAnnotation.intervalType())
.named("m"))
.addField(Types.optionalMap()
.key(Types.optional(INT32)
.as(LogicalTypeAnnotation.dateType())
.named("key"))
.value(Types.optional(BINARY)
.as(LogicalTypeAnnotation.stringType())
.named("value"))
.named("list"))
.named("root");

FileMetaData fmd = new FileMetaData(complexParquetSchema, Collections.emptyMap(), "ASF");
String prettyJSon = ParquetMetadata.toPrettyJSON(new ParquetMetadata(fmd, Collections.emptyList()));

assertEquals(mapper.readTree(prettyJSon), expectedJson());
}
}
Loading

0 comments on commit cc782d9

Please sign in to comment.