-
Notifications
You must be signed in to change notification settings - Fork 1.6k
PARQUET-2417: Add geometry and geography logical type annotations
#3200
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
Changes from 6 commits
9344ae9
a65945b
1022778
742a511
1e9f1f3
1f0f986
0eee71b
e3b9dbb
3304de6
89d8a74
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 |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| /* | ||
| * 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.column.schema; | ||
|
|
||
| /** | ||
| * Edge interpolation algorithm for Geography logical type | ||
| */ | ||
| public enum EdgeInterpolationAlgorithm { | ||
| SPHERICAL(0), | ||
| VINCENTY(1), | ||
| THOMAS(2), | ||
| ANDOYER(3), | ||
| KARNEY(4); | ||
|
|
||
| private final int value; | ||
|
|
||
| private EdgeInterpolationAlgorithm(int value) { | ||
| this.value = value; | ||
| } | ||
|
|
||
| /** | ||
| * Get the integer value of this enum value, as defined in the Thrift IDL. | ||
| */ | ||
| public int getValue() { | ||
| return value; | ||
| } | ||
|
|
||
| /** | ||
| * Find the enum type by its integer value, as defined in the Thrift IDL. | ||
| * @return null if the value is not found. | ||
| */ | ||
| public static EdgeInterpolationAlgorithm findByValue(int value) { | ||
| switch (value) { | ||
| case 0: | ||
| return SPHERICAL; | ||
| case 1: | ||
| return VINCENTY; | ||
| case 2: | ||
| return THOMAS; | ||
| case 3: | ||
| return ANDOYER; | ||
| case 4: | ||
| return KARNEY; | ||
| default: | ||
| return null; | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -41,6 +41,7 @@ | |||||||||||||||
| import java.util.Set; | ||||||||||||||||
| import java.util.function.Supplier; | ||||||||||||||||
| import org.apache.parquet.Preconditions; | ||||||||||||||||
| import org.apache.parquet.column.schema.EdgeInterpolationAlgorithm; | ||||||||||||||||
|
|
||||||||||||||||
| public abstract class LogicalTypeAnnotation { | ||||||||||||||||
| enum LogicalTypeToken { | ||||||||||||||||
|
|
@@ -155,6 +156,40 @@ protected LogicalTypeAnnotation fromString(List<String> params) { | |||||||||||||||
| return float16Type(); | ||||||||||||||||
| } | ||||||||||||||||
| }, | ||||||||||||||||
| GEOMETRY { | ||||||||||||||||
| @Override | ||||||||||||||||
| protected LogicalTypeAnnotation fromString(List<String> params) { | ||||||||||||||||
| if (params.size() > 1) { | ||||||||||||||||
| throw new RuntimeException("Expecting only crs for geometry logical type, got " + params.size()); | ||||||||||||||||
|
wgtmac marked this conversation as resolved.
Outdated
|
||||||||||||||||
| } | ||||||||||||||||
| if (params.size() == 1) { | ||||||||||||||||
| String crs = params.get(0); | ||||||||||||||||
| return geometryType(crs); | ||||||||||||||||
| } else { | ||||||||||||||||
| return geometryType(); | ||||||||||||||||
| } | ||||||||||||||||
|
wgtmac marked this conversation as resolved.
Outdated
|
||||||||||||||||
| } | ||||||||||||||||
| }, | ||||||||||||||||
| GEOGRAPHY { | ||||||||||||||||
| @Override | ||||||||||||||||
| protected LogicalTypeAnnotation fromString(List<String> params) { | ||||||||||||||||
| if (params.size() > 2) { | ||||||||||||||||
| throw new RuntimeException( | ||||||||||||||||
| "Expecting at most 2 parameters for geography logical type (crs and edgeAlgorithm), got " | ||||||||||||||||
|
wgtmac marked this conversation as resolved.
Outdated
|
||||||||||||||||
| + params.size()); | ||||||||||||||||
| } | ||||||||||||||||
| if (params.size() == 1) { | ||||||||||||||||
| String crs = params.get(0); | ||||||||||||||||
| return geographyType(crs, null); | ||||||||||||||||
| } | ||||||||||||||||
| if (params.size() == 2) { | ||||||||||||||||
| String crs = params.get(0); | ||||||||||||||||
| String edgeAlgorithm = params.get(1); | ||||||||||||||||
| return geographyType(crs, EdgeInterpolationAlgorithm.valueOf(edgeAlgorithm)); | ||||||||||||||||
| } | ||||||||||||||||
| return geographyType(); | ||||||||||||||||
|
wgtmac marked this conversation as resolved.
Outdated
|
||||||||||||||||
| } | ||||||||||||||||
| }, | ||||||||||||||||
| UNKNOWN { | ||||||||||||||||
| @Override | ||||||||||||||||
| protected LogicalTypeAnnotation fromString(List<String> params) { | ||||||||||||||||
|
|
@@ -334,6 +369,22 @@ public static Float16LogicalTypeAnnotation float16Type() { | |||||||||||||||
| return Float16LogicalTypeAnnotation.INSTANCE; | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| public static GeometryLogicalTypeAnnotation geometryType(String crs) { | ||||||||||||||||
| return new GeometryLogicalTypeAnnotation(crs); | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| public static GeometryLogicalTypeAnnotation geometryType() { | ||||||||||||||||
| return new GeometryLogicalTypeAnnotation("OGC:CRS84"); | ||||||||||||||||
| } | ||||||||||||||||
|
wgtmac marked this conversation as resolved.
Outdated
|
||||||||||||||||
|
|
||||||||||||||||
| public static GeographyLogicalTypeAnnotation geographyType(String crs, EdgeInterpolationAlgorithm edgeAlgorithm) { | ||||||||||||||||
| return new GeographyLogicalTypeAnnotation(crs, edgeAlgorithm); | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| public static GeographyLogicalTypeAnnotation geographyType() { | ||||||||||||||||
| return new GeographyLogicalTypeAnnotation("OGC:CRS84", null); | ||||||||||||||||
|
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.
Suggested change
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. It would be good to set both to null or explicit values for consistency. |
||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| public static UnknownLogicalTypeAnnotation unknownType() { | ||||||||||||||||
| return UnknownLogicalTypeAnnotation.INSTANCE; | ||||||||||||||||
| } | ||||||||||||||||
|
|
@@ -1183,6 +1234,136 @@ public boolean equals(Object obj) { | |||||||||||||||
| } | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| public static class GeometryLogicalTypeAnnotation extends LogicalTypeAnnotation { | ||||||||||||||||
| private final String crs; | ||||||||||||||||
|
|
||||||||||||||||
| private GeometryLogicalTypeAnnotation(String crs) { | ||||||||||||||||
| this.crs = crs; | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| @Override | ||||||||||||||||
| @Deprecated | ||||||||||||||||
| public OriginalType toOriginalType() { | ||||||||||||||||
| return null; | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| @Override | ||||||||||||||||
| public <T> Optional<T> accept(LogicalTypeAnnotationVisitor<T> logicalTypeAnnotationVisitor) { | ||||||||||||||||
| return logicalTypeAnnotationVisitor.visit(this); | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| @Override | ||||||||||||||||
| LogicalTypeToken getType() { | ||||||||||||||||
| return LogicalTypeToken.GEOMETRY; | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| @Override | ||||||||||||||||
| protected String typeParametersAsString() { | ||||||||||||||||
| StringBuilder sb = new StringBuilder(); | ||||||||||||||||
| sb.append("(").append(crs != null && !crs.isEmpty() ? crs : "").append(")"); | ||||||||||||||||
| return sb.toString(); | ||||||||||||||||
|
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.
Suggested change
This is more readable. |
||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| public String getCrs() { | ||||||||||||||||
| return crs; | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| @Override | ||||||||||||||||
| public boolean equals(Object obj) { | ||||||||||||||||
| if (!(obj instanceof GeometryLogicalTypeAnnotation)) { | ||||||||||||||||
| return false; | ||||||||||||||||
| } | ||||||||||||||||
| GeometryLogicalTypeAnnotation other = (GeometryLogicalTypeAnnotation) obj; | ||||||||||||||||
| return Objects.equals(crs, other.crs); | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| @Override | ||||||||||||||||
| public int hashCode() { | ||||||||||||||||
| return Objects.hash(crs); | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| @Override | ||||||||||||||||
| PrimitiveStringifier valueStringifier(PrimitiveType primitiveType) { | ||||||||||||||||
| return PrimitiveStringifier.WKB_STRINGIFIER; | ||||||||||||||||
| } | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| public static class GeographyLogicalTypeAnnotation extends LogicalTypeAnnotation { | ||||||||||||||||
| private final String crs; | ||||||||||||||||
| private final EdgeInterpolationAlgorithm edgeAlgorithm; | ||||||||||||||||
|
wgtmac marked this conversation as resolved.
Outdated
|
||||||||||||||||
|
|
||||||||||||||||
| private GeographyLogicalTypeAnnotation(String crs, EdgeInterpolationAlgorithm edgeAlgorithm) { | ||||||||||||||||
| this.crs = crs; | ||||||||||||||||
| this.edgeAlgorithm = edgeAlgorithm; | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| @Override | ||||||||||||||||
| @Deprecated | ||||||||||||||||
| public OriginalType toOriginalType() { | ||||||||||||||||
| return null; | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| @Override | ||||||||||||||||
| public <T> Optional<T> accept(LogicalTypeAnnotationVisitor<T> logicalTypeAnnotationVisitor) { | ||||||||||||||||
| return logicalTypeAnnotationVisitor.visit(this); | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| @Override | ||||||||||||||||
| LogicalTypeToken getType() { | ||||||||||||||||
| return LogicalTypeToken.GEOGRAPHY; | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| @Override | ||||||||||||||||
| protected String typeParametersAsString() { | ||||||||||||||||
| StringBuilder sb = new StringBuilder(); | ||||||||||||||||
|
|
||||||||||||||||
| boolean hasCrs = crs != null && !crs.isEmpty(); | ||||||||||||||||
| boolean hasEdgeAlgorithm = edgeAlgorithm != null; | ||||||||||||||||
|
|
||||||||||||||||
| if (!hasCrs && !hasEdgeAlgorithm) { | ||||||||||||||||
| return ""; // Return empty string when both are empty | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| sb.append("("); | ||||||||||||||||
| if (hasCrs) { | ||||||||||||||||
| sb.append(crs); | ||||||||||||||||
| } | ||||||||||||||||
| if (hasEdgeAlgorithm) { | ||||||||||||||||
| if (hasCrs) sb.append(","); | ||||||||||||||||
| sb.append(edgeAlgorithm); | ||||||||||||||||
| } | ||||||||||||||||
| sb.append(")"); | ||||||||||||||||
| return sb.toString(); | ||||||||||||||||
|
wgtmac marked this conversation as resolved.
Outdated
|
||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| public String getCrs() { | ||||||||||||||||
| return crs; | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| public EdgeInterpolationAlgorithm getEdgeAlgorithm() { | ||||||||||||||||
|
wgtmac marked this conversation as resolved.
Outdated
|
||||||||||||||||
| return edgeAlgorithm; | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| @Override | ||||||||||||||||
| public boolean equals(Object obj) { | ||||||||||||||||
| if (!(obj instanceof GeographyLogicalTypeAnnotation)) { | ||||||||||||||||
| return false; | ||||||||||||||||
| } | ||||||||||||||||
| GeographyLogicalTypeAnnotation other = (GeographyLogicalTypeAnnotation) obj; | ||||||||||||||||
| return Objects.equals(crs, other.crs) && Objects.equals(edgeAlgorithm, other.edgeAlgorithm); | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| @Override | ||||||||||||||||
| public int hashCode() { | ||||||||||||||||
| return Objects.hash(crs, edgeAlgorithm); | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| @Override | ||||||||||||||||
| PrimitiveStringifier valueStringifier(PrimitiveType primitiveType) { | ||||||||||||||||
| return PrimitiveStringifier.WKB_STRINGIFIER; | ||||||||||||||||
| } | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| /** | ||||||||||||||||
| * Implement this interface to visit a logical type annotation in the schema. | ||||||||||||||||
| * The default implementation for each logical type specific visitor method is empty. | ||||||||||||||||
|
|
@@ -1259,6 +1440,14 @@ default Optional<T> visit(Float16LogicalTypeAnnotation float16LogicalType) { | |||||||||||||||
| return empty(); | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| default Optional<T> visit(GeometryLogicalTypeAnnotation geometryLogicalType) { | ||||||||||||||||
| return empty(); | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| default Optional<T> visit(GeographyLogicalTypeAnnotation geographyLogicalType) { | ||||||||||||||||
| return empty(); | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| default Optional<T> visit(UnknownLogicalTypeAnnotation unknownLogicalTypeAnnotation) { | ||||||||||||||||
| return empty(); | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -35,6 +35,9 @@ | |||||||||||||||||
| import java.util.concurrent.TimeUnit; | ||||||||||||||||||
| import javax.naming.OperationNotSupportedException; | ||||||||||||||||||
| import org.apache.parquet.io.api.Binary; | ||||||||||||||||||
| import org.locationtech.jts.geom.Geometry; | ||||||||||||||||||
| import org.locationtech.jts.io.ParseException; | ||||||||||||||||||
| import org.locationtech.jts.io.WKBReader; | ||||||||||||||||||
|
|
||||||||||||||||||
| /** | ||||||||||||||||||
| * Class that provides string representations for the primitive values. These string values are to be used for | ||||||||||||||||||
|
|
@@ -442,6 +445,22 @@ private void appendHex(byte[] array, int offset, int length, StringBuilder build | |||||||||||||||||
| } | ||||||||||||||||||
| }; | ||||||||||||||||||
|
|
||||||||||||||||||
| static final PrimitiveStringifier WKB_STRINGIFIER = new BinaryStringifierBase("WKB_STRINGIFIER") { | ||||||||||||||||||
|
Contributor
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. @wgtmac and @zhangfengcdt, what do you think about updating this to use WKT only if JTS is available, rather than adding JTS to the classpath for all downstream clients? This could just produce a generic string if a geo library isn't available.
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. It makes sense for the printer. To implement geospatial statistics, we need to depend on JTS to parse WKB. Otherwise we could only blindly write them as blobs without producing any bbox and other stats. If this is acceptable, I think we can make such changes. @jiayuasu @szehon-ho WDYT?
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 believe it’s reasonable to add JTS as a dependency. Its license is compatible with the ASF, and it’s the most widely used geospatial library in the industry. The API is also very stable. JTS has been around for over 20 years. An alternative would be to implement a standalone WKB parser and maintain it within For context, the Parquet Geo C++ PR included a standalone WKB parser because (1) there isn’t a clean, well-maintained WKB parser in C++, and (2) Dewey had already implemented WKB parsing in C++ multiple times before. |
||||||||||||||||||
|
|
||||||||||||||||||
| @Override | ||||||||||||||||||
| String stringifyNotNull(Binary value) { | ||||||||||||||||||
|
|
||||||||||||||||||
| Geometry geometry; | ||||||||||||||||||
| try { | ||||||||||||||||||
| WKBReader reader = new WKBReader(); | ||||||||||||||||||
| geometry = reader.read(value.getBytesUnsafe()); | ||||||||||||||||||
|
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.
Suggested change
|
||||||||||||||||||
| return geometry.toText(); | ||||||||||||||||||
| } catch (ParseException e) { | ||||||||||||||||||
| return BINARY_INVALID; | ||||||||||||||||||
| } | ||||||||||||||||||
| } | ||||||||||||||||||
| }; | ||||||||||||||||||
|
|
||||||||||||||||||
| static final PrimitiveStringifier FLOAT16_STRINGIFIER = new BinaryStringifierBase("FLOAT16_STRINGIFIER") { | ||||||||||||||||||
|
|
||||||||||||||||||
| @Override | ||||||||||||||||||
|
|
||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.