-
Notifications
You must be signed in to change notification settings - Fork 1.6k
GH-3710: Tolerate unrecognized logical/physical type combinations when reading #3711
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -191,6 +191,15 @@ | |
| public class Types { | ||
| private static final int NOT_SET = 0; | ||
|
|
||
| /** | ||
| * Thrown when a logical type annotation is not applicable to a column's physical type. | ||
| */ | ||
| public static class UnsupportedLogicalTypeAnnotation extends IllegalStateException { | ||
| public UnsupportedLogicalTypeAnnotation(String message) { | ||
| super(message); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * A base builder for {@link Type} objects. | ||
| * | ||
|
|
@@ -344,6 +353,9 @@ public abstract static class BasePrimitiveBuilder<P, THIS extends BasePrimitiveB | |
| private int precision = NOT_SET; | ||
| private int scale = NOT_SET; | ||
| private ColumnOrder columnOrder; | ||
| // When true and an unsupported logical/physical type combination is encountered, the | ||
| // annotation is dropped and the column order is forced to "undefined" so stats are ignored. | ||
| private boolean dropUnsupportedLogicalTypeCombinations = false; | ||
|
|
||
| private BasePrimitiveBuilder(P parent, PrimitiveTypeName type) { | ||
| super(parent); | ||
|
|
@@ -426,8 +438,38 @@ public THIS columnOrder(ColumnOrder columnOrder) { | |
| return self(); | ||
| } | ||
|
|
||
| /** | ||
| * When set, an unsupported combination results in the logical type annotation being dropped | ||
| * rather than throwing. The associated statistics are also forcefully ignored by setting the | ||
| * column order to {@link ColumnOrderName#UNDEFINED}. | ||
| * | ||
| * @return this builder for method chaining | ||
| */ | ||
| public THIS dropUnsupportedLogicalTypeCombinations() { | ||
|
divjotarora marked this conversation as resolved.
|
||
| this.dropUnsupportedLogicalTypeCombinations = true; | ||
| return self(); | ||
| } | ||
|
|
||
| @Override | ||
| protected PrimitiveType build(String name) { | ||
| try { | ||
| return validateAndBuild(name); | ||
| } catch (UnsupportedLogicalTypeAnnotation e) { | ||
| if (!dropUnsupportedLogicalTypeCombinations) { | ||
| throw e; | ||
| } | ||
|
|
||
| LOGGER.warn( | ||
| "Dropping unsupported logical type annotation {} on physical type {}: {}", | ||
| logicalTypeAnnotation, | ||
| primitiveType, | ||
| e.getMessage()); | ||
| return new PrimitiveType( | ||
| repetition, primitiveType, length, name, null, null, id, ColumnOrder.undefined()); | ||
| } | ||
| } | ||
|
|
||
| private PrimitiveType validateAndBuild(String name) { | ||
| if (length == 0 && logicalTypeAnnotation instanceof LogicalTypeAnnotation.UUIDLogicalTypeAnnotation) { | ||
| length = LogicalTypeAnnotation.UUIDLogicalTypeAnnotation.BYTES; | ||
| } | ||
|
|
@@ -590,9 +632,15 @@ public Optional<Boolean> visit( | |
| return checkBinaryPrimitiveType(geographyLogicalType); | ||
| } | ||
|
|
||
| private void checkAnnotation(boolean valid, String message, Object... args) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd generally want to avoid passing a "boolean" into the "checkAnnotation" method. Since it doesn't actually check an annotation here, it just throws an exception if you pass through is false. I think this is a bit over-indexing on the previous "checkState" work and possibly hiding some other holes. I would probably try to re-think the whole visitor here rather than try to wrap a previous failure differently. Instead of what we have here maybe something like AllowedPhysicalTypes visitor /**
* The physical types a logical type annotation may annotate. An empty set means the annotation
* cannot be applied to a primitive type at all.
*/
private static final class AllowedPhysicalTypes {
private static final AllowedPhysicalTypes NONE =
new AllowedPhysicalTypes(EnumSet.noneOf(PrimitiveTypeName.class), NOT_SET);
private static final AllowedPhysicalTypes ANY =
new AllowedPhysicalTypes(EnumSet.allOf(PrimitiveTypeName.class), NOT_SET);
private final Set<PrimitiveTypeName> types;
private final int requiredLength;
static Optional<AllowedPhysicalTypes> of(PrimitiveTypeName... types) {
return Optional.of(new AllowedPhysicalTypes(EnumSet.copyOf(asList(types)), NOT_SET));
}
static Optional<AllowedPhysicalTypes> fixed(int requiredLength) {
return Optional.of(new AllowedPhysicalTypes(
EnumSet.of(PrimitiveTypeName.FIXED_LEN_BYTE_ARRAY), requiredLength));
}
boolean accepts(PrimitiveTypeName type, int length) {
return types.contains(type) && (requiredLength == NOT_SET || length == requiredLength);
}
@Override
public String toString() {
if (requiredLength != NOT_SET) {
return PrimitiveTypeName.FIXED_LEN_BYTE_ARRAY + "(" + requiredLength + ")";
}
return types.stream().map(Enum::name).collect(Collectors.joining(", "));
}
}private static final LogicalTypeAnnotationVisitor<AllowedPhysicalTypes> ALLOWED_PHYSICAL_TYPES =
new LogicalTypeAnnotationVisitor<AllowedPhysicalTypes>() {
@Override public Optional<AllowedPhysicalTypes> visit(StringLogicalTypeAnnotation t) { return of(BINARY); }
@Override public Optional<AllowedPhysicalTypes> visit(JsonLogicalTypeAnnotation t) { return of(BINARY); }
@Override public Optional<AllowedPhysicalTypes> visit(BsonLogicalTypeAnnotation t) { return of(BINARY); }
@Override public Optional<AllowedPhysicalTypes> visit(EnumLogicalTypeAnnotation t) { return of(BINARY); }
@Override public Optional<AllowedPhysicalTypes> visit(GeometryLogicalTypeAnnotation t) { return of(BINARY); }
@Override public Optional<AllowedPhysicalTypes> visit(GeographyLogicalTypeAnnotation t) { return of(BINARY); }
@Override public Optional<AllowedPhysicalTypes> visit(DateLogicalTypeAnnotation t) { return of(INT32); }
@Override public Optional<AllowedPhysicalTypes> visit(TimestampLogicalTypeAnnotation t) { return of(INT64); }
@Override public Optional<AllowedPhysicalTypes> visit(UUIDLogicalTypeAnnotation t) { return fixed(UUIDLogicalTypeAnnotation.BYTES); }
@Override public Optional<AllowedPhysicalTypes> visit(Float16LogicalTypeAnnotation t) { return fixed(Float16LogicalTypeAnnotation.BYTES); }
@Override public Optional<AllowedPhysicalTypes> visit(IntervalLogicalTypeAnnotation t) { return fixed(12); }
@Override public Optional<AllowedPhysicalTypes> visit(TimeLogicalTypeAnnotation t) {
return t.getUnit() == TimeUnit.MILLIS ? of(INT32) : of(INT64);
}
@Override public Optional<AllowedPhysicalTypes> visit(IntLogicalTypeAnnotation t) {
return t.getBitWidth() == 64 ? of(INT64) : of(INT32);
}
@Override public Optional<AllowedPhysicalTypes> visit(DecimalLogicalTypeAnnotation t) {
return of(INT32, INT64, BINARY, FIXED_LEN_BYTE_ARRAY);
}
@Override public Optional<AllowedPhysicalTypes> visit(UnknownLogicalTypeAnnotation t) {
return Optional.of(ANY);
}
};Then instead of trying to throw our way to control flow we can do something like if (logicalTypeAnnotation != null) {
AllowedPhysicalTypes allowed =
logicalTypeAnnotation.accept(ALLOWED_PHYSICAL_TYPES).orElse(AllowedPhysicalTypes.NONE);
if (!allowed.accepts(primitiveType, length)) {
if (!dropUnsupportedLogicalAnnotations) {
throw new IllegalStateException(allowed.isEmpty()
? logicalTypeAnnotation + " can not be applied to a primitive type"
: String.format("%s can only annotate %s", logicalTypeAnnotation, allowed));
}
LOGGER.warn(
"Dropping unsupported logical type annotation {} on physical type {}",
logicalTypeAnnotation, primitiveType);
return new PrimitiveType(
repetition, primitiveType, this.length, name, null, null, id, ColumnOrder.undefined());
}
validateDecimalPrecision(meta);
} |
||
| if (!valid) { | ||
| throw new UnsupportedLogicalTypeAnnotation(String.format(message, args)); | ||
| } | ||
| } | ||
|
|
||
| private Optional<Boolean> checkFixedPrimitiveType( | ||
| int l, LogicalTypeAnnotation logicalTypeAnnotation) { | ||
| Preconditions.checkState( | ||
| checkAnnotation( | ||
| primitiveType == PrimitiveTypeName.FIXED_LEN_BYTE_ARRAY && length == l, | ||
| "%s can only annotate FIXED_LEN_BYTE_ARRAY(%s)", | ||
| logicalTypeAnnotation, | ||
|
|
@@ -602,7 +650,7 @@ private Optional<Boolean> checkFixedPrimitiveType( | |
|
|
||
| private Optional<Boolean> checkBinaryPrimitiveType( | ||
| LogicalTypeAnnotation logicalTypeAnnotation) { | ||
| Preconditions.checkState( | ||
| checkAnnotation( | ||
| primitiveType == PrimitiveTypeName.BINARY, | ||
| "%s can only annotate BINARY", | ||
| logicalTypeAnnotation); | ||
|
|
@@ -611,7 +659,7 @@ private Optional<Boolean> checkBinaryPrimitiveType( | |
|
|
||
| private Optional<Boolean> checkInt32PrimitiveType( | ||
| LogicalTypeAnnotation logicalTypeAnnotation) { | ||
| Preconditions.checkState( | ||
| checkAnnotation( | ||
| primitiveType == PrimitiveTypeName.INT32, | ||
| "%s can only annotate INT32", | ||
| logicalTypeAnnotation); | ||
|
|
@@ -620,14 +668,14 @@ private Optional<Boolean> checkInt32PrimitiveType( | |
|
|
||
| private Optional<Boolean> checkInt64PrimitiveType( | ||
| LogicalTypeAnnotation logicalTypeAnnotation) { | ||
| Preconditions.checkState( | ||
| checkAnnotation( | ||
| primitiveType == PrimitiveTypeName.INT64, | ||
| "%s can only annotate INT64", | ||
| logicalTypeAnnotation); | ||
| return Optional.of(true); | ||
| } | ||
| }) | ||
| .orElseThrow(() -> new IllegalStateException( | ||
| .orElseThrow(() -> new UnsupportedLogicalTypeAnnotation( | ||
| logicalTypeAnnotation + " can not be applied to a primitive type")); | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1605,4 +1605,17 @@ public void testGeographyLogicalTypeWithoutEdgeInterpolationAlgorithm() { | |
| Types.optional(BINARY).as(LogicalTypeAnnotation.geographyType()).named("aGeography"); | ||
| assertThat(optionalGeographyActual).isEqualTo(optionalGeographyExpected); | ||
| } | ||
|
|
||
| @Test | ||
| public void testDropUnsupportedLogicalTypeCombinations() { | ||
| // Other tests already validate that unsupported type combinations throw by default, so this | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd probably drop this description? |
||
| // test only validates that the dropUnsupportedLogicalTypeCombinations flag works. | ||
| PrimitiveType pt = Types.required(BOOLEAN) | ||
| .dropUnsupportedLogicalTypeCombinations() | ||
| .as(LogicalTypeAnnotation.timestampType(true, MILLIS)) | ||
| .named("bool_ts"); | ||
| assertThat(pt.getPrimitiveTypeName()).isEqualTo(BOOLEAN); | ||
| assertThat(pt.getLogicalTypeAnnotation()).isNull(); // Dropped | ||
| assertThat(pt.columnOrder().getColumnOrderName()).isEqualTo(ColumnOrder.ColumnOrderName.UNDEFINED); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| /* | ||
| * 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; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| import org.apache.hadoop.conf.Configuration; | ||
| import org.apache.hadoop.fs.Path; | ||
| import org.apache.parquet.example.data.Group; | ||
| import org.apache.parquet.hadoop.example.GroupReadSupport; | ||
| import org.apache.parquet.hadoop.metadata.ParquetMetadata; | ||
| import org.apache.parquet.hadoop.util.HadoopInputFile; | ||
| import org.apache.parquet.schema.ColumnOrder.ColumnOrderName; | ||
| import org.apache.parquet.schema.PrimitiveType; | ||
| import org.apache.parquet.schema.PrimitiveType.PrimitiveTypeName; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| public class TestReadInvalidTypeCombination { | ||
|
|
||
| // parquet-testing file with an invalid logical/physical type combination. | ||
| private static final String REFERENCE_FILE = "int32_with_uuid_logical_type.parquet"; | ||
| private static final String REFERENCE_CHANGESET = "4b1ce4502afff8d20c9b4bb08d07e04e21cdeff3"; | ||
|
|
||
| private final InterOpTester interop = new InterOpTester(); | ||
|
|
||
| @Test | ||
| public void testReadInvalidTypeCombinationSucceeds() throws Exception { | ||
| Configuration conf = new Configuration(); | ||
| Path file = interop.GetInterOpFile(REFERENCE_FILE, REFERENCE_CHANGESET); | ||
|
|
||
| // The footer parse should succeed and drop the annotation and stats for the column. | ||
| try (ParquetFileReader reader = ParquetFileReader.open(HadoopInputFile.fromPath(file, conf))) { | ||
| ParquetMetadata footer = reader.getFooter(); | ||
| PrimitiveType column = | ||
| footer.getFileMetaData().getSchema().getType("int32_uuid").asPrimitiveType(); | ||
|
|
||
| assertThat(column.getPrimitiveTypeName()).isEqualTo(PrimitiveTypeName.INT32); | ||
| assertThat(column.getLogicalTypeAnnotation()).isNull(); | ||
| assertThat(column.columnOrder().getColumnOrderName()).isEqualTo(ColumnOrderName.UNDEFINED); | ||
| } | ||
|
|
||
| // The physical values are still fully readable. | ||
| int rows = 0; | ||
| try (ParquetReader<Group> reader = ParquetReader.builder(new GroupReadSupport(), file) | ||
| .withConf(conf) | ||
| .build()) { | ||
| Group g; | ||
| while ((g = reader.read()) != null) { | ||
| assertThat(g.getInteger("int32_uuid", 0)).isEqualTo(rows); | ||
| rows++; | ||
| } | ||
| } | ||
| assertThat(rows).isEqualTo(10); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a load bearing comment :) I would probably skip all this since it's a implementation detail that can go stale pretty quickly. Instead stick to the behavior.
For example you could say "ignoreUnsupportedLogicalAnnotations" as the variable name and I think that would cover it? Or "dropUnsupportedLogicalAnnotations"?