Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PARQUET-138: Allow merging more restrictive field in less restrictive field #550

Merged
merged 2 commits into from
Feb 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -400,9 +400,6 @@ List<Type> mergeFields(GroupType toMerge, boolean strict) {
Type merged;
if (toMerge.containsField(type.getName())) {
Type fieldToMerge = toMerge.getType(type.getName());
if (fieldToMerge.getRepetition().isMoreRestrictiveThan(type.getRepetition())) {
throw new IncompatibleSchemaModificationException("repetition constraint is more restrictive: can not merge type " + fieldToMerge + " into " + type);
}
if (type.getLogicalTypeAnnotation() != null && !type.getLogicalTypeAnnotation().equals(fieldToMerge.getLogicalTypeAnnotation())) {
throw new IncompatibleSchemaModificationException("cannot merge logical type " + fieldToMerge.getLogicalTypeAnnotation() + " into " + type.getLogicalTypeAnnotation());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -750,7 +750,8 @@ protected Type union(Type toMerge, boolean strict) {
}
}

Types.PrimitiveBuilder<PrimitiveType> builder = Types.primitive(primitive, toMerge.getRepetition());
Repetition repetition = Repetition.leastRestrictive(this.getRepetition(), toMerge.getRepetition());
Types.PrimitiveBuilder<PrimitiveType> builder = Types.primitive(primitive, repetition);

if (PrimitiveTypeName.FIXED_LEN_BYTE_ARRAY == primitive) {
builder.length(length);
Expand Down
23 changes: 23 additions & 0 deletions parquet-column/src/main/java/org/apache/parquet/schema/Type.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import static org.apache.parquet.Preconditions.checkNotNull;

import java.util.Arrays;
import java.util.List;

import org.apache.parquet.io.InvalidRecordException;
Expand Down Expand Up @@ -111,6 +112,28 @@ public boolean isMoreRestrictiveThan(Repetition other) {
*/
abstract public boolean isMoreRestrictiveThan(Repetition other);


/**
* @param repetitions repetitions to traverse
* @return the least restrictive repetition of all repetitions provided
*/
public static Repetition leastRestrictive(Repetition... repetitions) {
boolean hasOptional = false;

for (Repetition repetition : repetitions) {
if (repetition == REPEATED) {
return REPEATED;
} else if (repetition == OPTIONAL) {
hasOptional = true;
}
}

if (hasOptional) {
return OPTIONAL;
}

return REQUIRED;
}
}

private final String name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@

import static org.apache.parquet.schema.PrimitiveType.PrimitiveTypeName.FIXED_LEN_BYTE_ARRAY;
import static org.apache.parquet.schema.OriginalType.LIST;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import static org.apache.parquet.schema.PrimitiveType.PrimitiveTypeName.BINARY;
import static org.apache.parquet.schema.PrimitiveType.PrimitiveTypeName.INT32;
import static org.apache.parquet.schema.PrimitiveType.PrimitiveTypeName.INT96;
import static org.apache.parquet.schema.Type.Repetition.OPTIONAL;
import static org.apache.parquet.schema.Type.Repetition.REPEATED;
import static org.apache.parquet.schema.Type.Repetition.REQUIRED;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import org.junit.Test;

Expand Down Expand Up @@ -89,12 +89,11 @@ public void testMergeSchema() {
MessageType t4 = new MessageType("root2",
new PrimitiveType(REQUIRED, BINARY, "a"));

try {
t3.union(t4);
fail("moving from optional to required");
} catch (IncompatibleSchemaModificationException e) {
assertEquals("repetition constraint is more restrictive: can not merge type required binary a into optional binary a", e.getMessage());
}
assertEquals(
t3.union(t4),
new MessageType("root1",
new PrimitiveType(OPTIONAL, BINARY, "a"))
);

assertEquals(
t4.union(t3),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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.schema;

import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class TestRepetitionType {
@Test
public void testLeastRestrictiveRepetition() {
Type.Repetition REQUIRED = Type.Repetition.REQUIRED;
Type.Repetition OPTIONAL = Type.Repetition.OPTIONAL;
Type.Repetition REPEATED = Type.Repetition.REPEATED;

assertEquals(REPEATED, Type.Repetition.leastRestrictive(REQUIRED, OPTIONAL, REPEATED, REQUIRED, OPTIONAL, REPEATED));
assertEquals(OPTIONAL, Type.Repetition.leastRestrictive(REQUIRED, OPTIONAL, REQUIRED, OPTIONAL));
assertEquals(REQUIRED, Type.Repetition.leastRestrictive(REQUIRED, REQUIRED));
}
}