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-2417: Add support for geometry logical type #2971

Open
wants to merge 24 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
6a2e051
PARQUET-2471: Add support for geometry logical type
zhangfengcdt Jul 23, 2024
d354012
fix types
zhangfengcdt Jul 23, 2024
969b696
Refactor BoundingBox and GeometryTypes
zhangfengcdt Jul 23, 2024
87ee8ea
revert naming changes
zhangfengcdt Jul 24, 2024
d67e03b
revert TestDecimalUtils
zhangfengcdt Jul 24, 2024
ccf1c4a
revert more
zhangfengcdt Jul 24, 2024
35342c2
refactor statistics
zhangfengcdt Jul 24, 2024
bcfefb7
modify EnvelopeCovering
zhangfengcdt Jul 24, 2024
cf615f6
add more unit tests
zhangfengcdt Jul 25, 2024
c6ae733
update comments
zhangfengcdt Jul 25, 2024
80a629e
add comment for envelope converging expand calculation
zhangfengcdt Jul 26, 2024
e0ec9ef
Fix the boundingbox initial values in constructor
zhangfengcdt Jul 29, 2024
7c728b1
Update the poc implementation for the changes to the spec
zhangfengcdt Aug 7, 2024
fa36d06
remove print
zhangfengcdt Aug 7, 2024
2a1c62a
implement evelop covering for spherical coordinates
zhangfengcdt Aug 8, 2024
d0e7d3d
throw a not-implemented exception for the covering statistics when th…
zhangfengcdt Aug 12, 2024
30d64be
Merge branch 'master' of github.com:apache/parquet-java into feature-…
zhangfengcdt Aug 12, 2024
29a86b5
remove unused comment codes
zhangfengcdt Aug 12, 2024
0c4b8b4
address some review comments
zhangfengcdt Aug 19, 2024
a56e9ad
revert changes that are not desired
zhangfengcdt Aug 20, 2024
1ae3d99
refactor toString and remove test scope of jts-core in parquet-hadoop…
zhangfengcdt Aug 20, 2024
198642d
refactor the converings to be map to avoid ordering issues in stats m…
zhangfengcdt Aug 23, 2024
0cf2de9
address review comments
zhangfengcdt Sep 13, 2024
4f0f7ed
fix formating
zhangfengcdt Sep 13, 2024
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
12 changes: 12 additions & 0 deletions parquet-column/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,18 @@
<version>${slf4j.version}</version>
</dependency>

<dependency>
<groupId>org.locationtech.jts</groupId>
<artifactId>jts-core</artifactId>
<version>${jts.version}</version>
</dependency>

<dependency>
<groupId>net.sf.geographiclib</groupId>
<artifactId>GeographicLib-Java</artifactId>
zhangfengcdt marked this conversation as resolved.
Show resolved Hide resolved
<version>1.50</version>
</dependency>

<dependency>
<groupId>com.carrotsearch</groupId>
<artifactId>junit-benchmarks</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
*/
package org.apache.parquet.column.statistics;

import org.apache.parquet.column.statistics.geometry.GeometryStatistics;
import org.apache.parquet.io.api.Binary;
import org.apache.parquet.schema.LogicalTypeAnnotation;
import org.apache.parquet.schema.PrimitiveType;
import org.apache.parquet.schema.Types;

Expand All @@ -30,6 +32,7 @@ public class BinaryStatistics extends Statistics<Binary> {

private Binary max;
private Binary min;
private GeometryStatistics geometryStatistics = null;

/**
* @deprecated will be removed in 2.0.0. Use {@link Statistics#createStats(org.apache.parquet.schema.Type)} instead
Expand All @@ -41,6 +44,13 @@ public BinaryStatistics() {

BinaryStatistics(PrimitiveType type) {
super(type);
LogicalTypeAnnotation logicalType = type.getLogicalTypeAnnotation();
if (logicalType instanceof LogicalTypeAnnotation.GeometryLogicalTypeAnnotation) {
LogicalTypeAnnotation.GeometryLogicalTypeAnnotation geometryLogicalType =
(LogicalTypeAnnotation.GeometryLogicalTypeAnnotation) logicalType;
geometryStatistics = new GeometryStatistics(
geometryLogicalType.getEdges(), geometryLogicalType.getCrs(), geometryLogicalType.getMetadata());
}
}

private BinaryStatistics(BinaryStatistics other) {
Expand All @@ -49,6 +59,9 @@ private BinaryStatistics(BinaryStatistics other) {
initializeStats(other.min, other.max);
}
setNumNulls(other.getNumNulls());
if (other.geometryStatistics != null) {
geometryStatistics = other.geometryStatistics.copy();
}
}

@Override
Expand All @@ -62,6 +75,9 @@ public void updateStats(Binary value) {
} else if (comparator().compare(max, value) < 0) {
max = value.copy();
}
if (geometryStatistics != null) {
geometryStatistics.update(value);
}
}

@Override
Expand All @@ -72,6 +88,9 @@ public void mergeStatisticsMinMax(Statistics stats) {
} else {
updateStats(binaryStats.getMin(), binaryStats.getMax());
}
if (geometryStatistics != null) {
geometryStatistics.merge(binaryStats.geometryStatistics);
}
}

/**
Expand Down Expand Up @@ -190,4 +209,12 @@ public void setMinMax(Binary min, Binary max) {
public BinaryStatistics copy() {
return new BinaryStatistics(this);
}

public void setGeometryStatistics(GeometryStatistics geometryStatistics) {
this.geometryStatistics = geometryStatistics;
}

public GeometryStatistics getGeometryStatistics() {
return geometryStatistics;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.util.Arrays;
import org.apache.parquet.column.UnknownColumnTypeException;
import org.apache.parquet.column.statistics.geometry.GeometryStatistics;
import org.apache.parquet.io.api.Binary;
import org.apache.parquet.schema.Float16;
import org.apache.parquet.schema.LogicalTypeAnnotation;
Expand Down Expand Up @@ -64,6 +65,10 @@ public Builder withNumNulls(long numNulls) {
return this;
}

public Builder withGeometryStatistics(GeometryStatistics geometryStatistics) {
throw new UnsupportedOperationException("Please use the GeometryBuilder");
}

public Statistics<?> build() {
Statistics<?> stats = createStats(type);
if (min != null && max != null) {
Expand Down Expand Up @@ -178,6 +183,30 @@ public Statistics<?> build() {
}
}

// Builder for GEOMETRY type to handle GeometryStatistics
private static class GeometryBuilder extends Builder {

private GeometryStatistics geometryStatistics;

public GeometryBuilder(PrimitiveType type) {
super(type);
assert type.getPrimitiveTypeName() == PrimitiveTypeName.BINARY;
}

@Override
public Builder withGeometryStatistics(GeometryStatistics geometryStatistics) {
this.geometryStatistics = geometryStatistics;
return this;
}

@Override
public Statistics<?> build() {
BinaryStatistics stats = (BinaryStatistics) super.build();
stats.setGeometryStatistics(geometryStatistics);
return stats;
}
}

private final PrimitiveType type;
private final PrimitiveComparator<T> comparator;
private boolean hasNonNullValue;
Expand Down Expand Up @@ -269,6 +298,11 @@ public static Builder getBuilderForReading(PrimitiveType type) {
if (logicalTypeAnnotation instanceof LogicalTypeAnnotation.Float16LogicalTypeAnnotation) {
return new Float16Builder(type);
}
return new Builder(type);
case BINARY:
if (type.getLogicalTypeAnnotation() instanceof LogicalTypeAnnotation.GeometryLogicalTypeAnnotation) {
return new GeometryBuilder(type);
}
default:
return new Builder(type);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
/*
* 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.statistics.geometry;

import org.apache.parquet.Preconditions;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.Envelope;
import org.locationtech.jts.geom.Geometry;

public class BoundingBox {

private double xMin = Double.POSITIVE_INFINITY;
private double xMax = Double.NEGATIVE_INFINITY;
private double yMin = Double.POSITIVE_INFINITY;
private double yMax = Double.NEGATIVE_INFINITY;
private double zMin = Double.POSITIVE_INFINITY;
private double zMax = Double.NEGATIVE_INFINITY;
private double mMin = Double.POSITIVE_INFINITY;
private double mMax = Double.NEGATIVE_INFINITY;

public BoundingBox(
double xMin, double xMax, double yMin, double yMax, double zMin, double zMax, double mMin, double mMax) {
this.xMin = xMin;
this.xMax = xMax;
this.yMin = yMin;
this.yMax = yMax;
this.zMin = zMin;
this.zMax = zMax;
this.mMin = mMin;
this.mMax = mMax;
}

public BoundingBox() {}

public double getXMin() {
return xMin;
}

public double getXMax() {
return xMax;
}

public double getYMin() {
return yMin;
}

public double getYMax() {
return yMax;
}

public double getZMin() {
return zMin;
}

public double getZMax() {
return zMax;
}

public double getMMin() {
return mMin;
}

public double getMMax() {
return mMax;
}

void update(double minX, double maxX, double minY, double maxY, double minZ, double maxZ) {
xMin = Math.min(xMin, minX);
yMin = Math.min(yMin, minY);
xMax = Math.max(xMax, maxX);
yMax = Math.max(yMax, maxY);
zMin = Math.min(zMin, minZ);
zMax = Math.max(zMax, maxZ);
}

void update(Geometry geometry) {
zhangfengcdt marked this conversation as resolved.
Show resolved Hide resolved
GeometryUtils.normalizeLongitude(geometry);
Envelope envelope = geometry.getEnvelopeInternal();
double minX = envelope.getMinX();
double minY = envelope.getMinY();
double maxX = envelope.getMaxX();
double maxY = envelope.getMaxY();

// JTS (Java Topology Suite) does not handle Z-coordinates directly in the Envelope class
// because it's primarily used for 2D geometries. However, we can iterate through the
// coordinates of the geometry to find the minimum and maximum Z values.
double minZ = Double.POSITIVE_INFINITY;
double maxZ = Double.NEGATIVE_INFINITY;

Coordinate[] coordinates = geometry.getCoordinates();
for (Coordinate coord : coordinates) {
if (!Double.isNaN(coord.getZ())) {
// Update zMin and zMax by iterating through the coordinates.
minZ = Math.min(minZ, coord.getZ());
maxZ = Math.max(maxZ, coord.getZ());
}
}

update(minX, maxX, minY, maxY, minZ, maxZ);
}

// Method to merge a Geometry object into this bounding box
public void merge(Geometry geometry) {
zhangfengcdt marked this conversation as resolved.
Show resolved Hide resolved
Preconditions.checkArgument(geometry != null, "Cannot merge with null geometry");
GeometryUtils.normalizeLongitude(geometry);
zhangfengcdt marked this conversation as resolved.
Show resolved Hide resolved
update(geometry);
}

public void merge(BoundingBox other) {
Preconditions.checkArgument(other != null, "Cannot merge with null bounding box");
xMin = Math.min(xMin, other.xMin);
xMax = Math.max(xMax, other.xMax);
yMin = Math.min(yMin, other.yMin);
yMax = Math.max(yMax, other.yMax);
zMin = Math.min(zMin, other.zMin);
zMax = Math.max(zMax, other.zMax);
mMin = Math.min(mMin, other.mMin);
mMax = Math.max(mMax, other.mMax);
}

public void reset() {
xMin = Double.POSITIVE_INFINITY;
xMax = Double.NEGATIVE_INFINITY;
yMin = Double.POSITIVE_INFINITY;
yMax = Double.NEGATIVE_INFINITY;
zMin = Double.POSITIVE_INFINITY;
zMax = Double.NEGATIVE_INFINITY;
mMin = Double.POSITIVE_INFINITY;
mMax = Double.NEGATIVE_INFINITY;
}

public void abort() {
xMin = Double.NaN;
xMax = Double.NaN;
yMin = Double.NaN;
yMax = Double.NaN;
zMin = Double.NaN;
zMax = Double.NaN;
mMin = Double.NaN;
mMax = Double.NaN;
}

public BoundingBox copy() {
return new BoundingBox(xMin, xMax, yMin, yMax, zMin, zMax, mMin, mMax);
}

@Override
public String toString() {
return "BoundingBox{" + "xMin="
+ xMin + ", xMax="
+ xMax + ", yMin="
+ yMin + ", yMax="
+ yMax + ", zMin="
+ zMin + ", zMax="
+ zMax + ", mMin="
+ mMin + ", mMax="
+ mMax + '}';
}
}
Loading