diff --git a/python/pyarrow/_parquet.pxd b/python/pyarrow/_parquet.pxd --- a/python/pyarrow/_parquet.pxd +++ b/python/pyarrow/_parquet.pxd @@ -68,6 +68,8 @@ cdef extern from "parquet/api/schema.h" namespace "parquet" nogil: ParquetLogicalType_JSON" parquet::LogicalType::Type::JSON" ParquetLogicalType_BSON" parquet::LogicalType::Type::BSON" ParquetLogicalType_UUID" parquet::LogicalType::Type::UUID" + ParquetLogicalType_GEOMETRY" parquet::LogicalType::Type::GEOMETRY" + ParquetLogicalType_GEOGRAPHY" parquet::LogicalType::Type::GEOGRAPHY" ParquetLogicalType_NONE" parquet::LogicalType::Type::NONE" enum ParquetTimeUnit" parquet::LogicalType::TimeUnit::unit": @@ -76,6 +78,14 @@ cdef extern from "parquet/api/schema.h" namespace "parquet" nogil: ParquetTimeUnit_MICROS" parquet::LogicalType::TimeUnit::MICROS" ParquetTimeUnit_NANOS" parquet::LogicalType::TimeUnit::NANOS" + enum ParquetEdgeInterpolationAlgorithm" parquet::LogicalType::EdgeInterpolationAlgorithm": + ParquetEdgeInterpolationAlgorithm_UNKNOWN" parquet::LogicalType::EdgeInterpolationAlgorithm::UNKNOWN" + ParquetEdgeInterpolationAlgorithm_SPHERICAL" parquet::LogicalType::EdgeInterpolationAlgorithm::SPHERICAL" + ParquetEdgeInterpolationAlgorithm_VINCENTY" parquet::LogicalType::EdgeInterpolationAlgorithm::VINCENTY" + ParquetEdgeInterpolationAlgorithm_THOMAS" parquet::LogicalType::EdgeInterpolationAlgorithm::THOMAS" + ParquetEdgeInterpolationAlgorithm_ANDOYER" parquet::LogicalType::EdgeInterpolationAlgorithm::ANDOYER" + ParquetEdgeInterpolationAlgorithm_KARNEY" parquet::LogicalType::EdgeInterpolationAlgorithm::KARNEY" + enum ParquetConvertedType" parquet::ConvertedType::type": ParquetConvertedType_NONE" parquet::ConvertedType::NONE" ParquetConvertedType_UTF8" parquet::ConvertedType::UTF8" @@ -167,6 +177,15 @@ cdef extern from "parquet/api/schema.h" namespace "parquet" nogil: c_bool is_adjusted_to_utc() const ParquetTimeUnit time_unit() const + cdef cppclass CParquetGeometryType \ + " parquet::GeometryLogicalType"(CParquetLogicalType): + c_string crs() const + + cdef cppclass CParquetGeographyType \ + " parquet::GeographyLogicalType"(CParquetLogicalType): + c_string crs() const + ParquetEdgeInterpolationAlgorithm algorithm() const + cdef cppclass ColumnDescriptor" parquet::ColumnDescriptor": c_bool Equals(const ColumnDescriptor& other) @@ -205,6 +224,33 @@ cdef extern from "parquet/api/schema.h" namespace "parquet" nogil: ParquetCipher algorithm AadMetadata aad + +# Specific array<> types needed for GeoStatistics +cdef extern from "" namespace "std" nogil: + cdef cppclass double_array4 "std::array": + double_array4() except + + double& operator[](size_t) + + cdef cppclass bool_array4 "std::array": + bool_array4() except + + c_bool& operator[](size_t) + + +cdef extern from "parquet/geospatial/statistics.h" namespace "parquet" nogil: + + cdef cppclass CParquetGeoStatistics" parquet::geospatial::GeoStatistics": + c_bool is_valid() const + + double_array4 lower_bound() const + double_array4 upper_bound() const + bool_array4 dimension_valid() const + bool_array4 dimension_empty() const + + optional[vector[int32_t]] geometry_types() const + + c_string ToString() const + + cdef extern from "parquet/api/reader.h" namespace "parquet" nogil: cdef cppclass ColumnReader: pass @@ -314,6 +360,8 @@ cdef extern from "parquet/api/reader.h" namespace "parquet" nogil: shared_ptr[ColumnPath] path_in_schema() const bint is_stats_set() const shared_ptr[CStatistics] statistics() const + c_bool is_geo_stats_set() const + shared_ptr[CParquetGeoStatistics] geo_statistics() const ParquetCompression compression() const const vector[ParquetEncoding]& encodings() const c_bool Equals(const CColumnChunkMetaData&) const @@ -662,6 +710,16 @@ cdef class Statistics(_Weakrefable): self.statistics = statistics self.parent = parent +cdef class GeoStatistics(_Weakrefable): + cdef: + shared_ptr[CParquetGeoStatistics] statistics + ColumnChunkMetaData parent + + cdef inline init(self, const shared_ptr[CParquetGeoStatistics]& statistics, + ColumnChunkMetaData parent): + self.statistics = statistics + self.parent = parent + cdef extern from "parquet/encryption/encryption.h" namespace "parquet" nogil: cdef cppclass CFileDecryptionProperties\ " parquet::FileDecryptionProperties": diff --git a/python/pyarrow/_parquet.pyx b/python/pyarrow/_parquet.pyx --- a/python/pyarrow/_parquet.pyx +++ b/python/pyarrow/_parquet.pyx @@ -319,6 +319,130 @@ cdef _box_flba(ParquetFLBA val, uint32_t len): return cp.PyBytes_FromStringAndSize( val.ptr, len) +cdef class GeoStatistics(_Weakrefable): + """Statistics for columns with geospatial data types (experimental) + + These statistics provide a bounding box and list of geometry types for + geospatial columns (GEOMETRY or GEOGRAPHY). All components may be None + if a file does not provide information about a particular component. + """ + + def __init__(self): + raise TypeError(f"Do not call {self.__class__.__name__}'s constructor directly") + + def __cinit__(self): + pass + + def __repr__(self): + return f"""{object.__repr__(self)} + geospatial_types: {self.geospatial_types} + xmin: {self.xmin}, xmax: {self.xmax} + ymin: {self.ymin}, ymax: {self.ymax} + zmin: {self.zmin}, zmax: {self.zmax} + mmin: {self.mmin}, mmax: {self.mmax}""" + + def to_dict(self): + """Dictionary summary of these statistics""" + out = { + "geospatial_types": self.geospatial_types, + "xmin": self.xmin, + "xmax": self.xmax, + "ymin": self.ymin, + "ymax": self.ymax, + "zmin": self.zmin, + "zmax": self.zmax, + "mmin": self.mmin, + "mmax": self.mmax + } + + return out + + @property + def geospatial_types(self): + """Unique geometry types + + Contains an integer code for each geometry type code encountered in the + geometries represented by these statistics. The geometry type codes are + ISO WKB geometry type codes returned in sorted order without duplicates. + + This property may be None if geospatial types are not available. + """ + cdef optional[vector[int32_t]] maybe_geometry_types = \ + self.statistics.get().geometry_types() + if not maybe_geometry_types.has_value(): + return None + + return list(maybe_geometry_types.value()) + + @property + def xmin(self): + """Minimum X value or None if not available + + Note that Parquet permits "wraparound" bounds in the X direction only + to more compactly represent bounds for geometries with components on + both sides of the antimeridian. This case is denoted by xmin > xmax. + """ + return self.statistics.get().lower_bound()[0] if self._x_valid() else None + + @property + def xmax(self): + """Maximum X value or None if not available + + Note that Parquet permits "wraparound" bounds in the X direction only + (see xmin). + """ + return self.statistics.get().upper_bound()[0] if self._x_valid() else None + + @property + def ymin(self): + """Minimum Y value or None if not available""" + return self.statistics.get().lower_bound()[1] if self._y_valid() else None + + @property + def ymax(self): + """Maximum Y value or None if not available""" + return self.statistics.get().upper_bound()[1] if self._y_valid() else None + + @property + def zmin(self): + """Minimum Z value or None if not available""" + return self.statistics.get().lower_bound()[2] if self._z_valid() else None + + @property + def zmax(self): + """Maximum Z value or None if not available""" + return self.statistics.get().upper_bound()[2] if self._z_valid() else None + + @property + def mmin(self): + """Minimum M value or None if not available""" + return self.statistics.get().lower_bound()[3] if self._m_valid() else None + + @property + def mmax(self): + """Maximum M value or None if not available""" + return self.statistics.get().upper_bound()[3] if self._m_valid() else None + + # Helpers to calculate the availability of a given dimension. For statistics + # read from a file, dimension_empty should always be false because there is + # no way to represent an empty range in Thrift; however, we check to be safe. + def _x_valid(self): + return self.statistics.get().dimension_valid()[0] \ + and not self.statistics.get().dimension_empty()[0] + + def _y_valid(self): + return self.statistics.get().dimension_valid()[1] \ + and not self.statistics.get().dimension_empty()[1] + + def _z_valid(self): + return self.statistics.get().dimension_valid()[2] \ + and not self.statistics.get().dimension_empty()[2] + + def _m_valid(self): + return self.statistics.get().dimension_valid()[3] \ + and not self.statistics.get().dimension_empty()[3] + + cdef class ColumnChunkMetaData(_Weakrefable): """Column metadata for a single row group.""" @@ -330,6 +454,7 @@ cdef class ColumnChunkMetaData(_Weakrefable): def __repr__(self): statistics = indent(repr(self.statistics), 4 * ' ') + geo_statistics = indent(repr(self.geo_statistics), 4 * ' ') return """{0} file_offset: {1} file_path: {2} @@ -339,20 +464,23 @@ cdef class ColumnChunkMetaData(_Weakrefable): is_stats_set: {6} statistics: {7} - compression: {8} - encodings: {9} - has_dictionary_page: {10} - dictionary_page_offset: {11} - data_page_offset: {12} - total_compressed_size: {13} - total_uncompressed_size: {14}""".format(object.__repr__(self), + geo_statistics: +{8} + compression: {9} + encodings: {10} + has_dictionary_page: {11} + dictionary_page_offset: {12} + data_page_offset: {13} + total_compressed_size: {14} + total_uncompressed_size: {15}""".format(object.__repr__(self), self.file_offset, self.file_path, self.physical_type, self.num_values, self.path_in_schema, self.is_stats_set, statistics, + geo_statistics, self.compression, self.encodings, self.has_dictionary_page, @@ -371,6 +499,11 @@ cdef class ColumnChunkMetaData(_Weakrefable): Dictionary with a key for each attribute of this class. """ statistics = self.statistics.to_dict() if self.is_stats_set else None + if self.is_geo_stats_set: + geo_statistics = self.geo_statistics.to_dict() + else: + geo_statistics = None + d = dict( file_offset=self.file_offset, file_path=self.file_path, @@ -379,6 +512,7 @@ cdef class ColumnChunkMetaData(_Weakrefable): path_in_schema=self.path_in_schema, is_stats_set=self.is_stats_set, statistics=statistics, + geo_statistics=geo_statistics, compression=self.compression, encodings=self.encodings, has_dictionary_page=self.has_dictionary_page, @@ -450,6 +584,21 @@ cdef class ColumnChunkMetaData(_Weakrefable): statistics.init(self.metadata.statistics(), self) return statistics + @property + def is_geo_stats_set(self): + """Whether or not geometry statistics are present in metadata (bool).""" + return self.metadata.is_geo_stats_set() + + @property + def geo_statistics(self): + """Statistics for column chunk (:class:`GeoStatistics`).""" + c_geo_statistics = self.metadata.geo_statistics() + if not c_geo_statistics or not c_geo_statistics.get().is_valid(): + return None + cdef GeoStatistics geo_statistics = GeoStatistics.__new__(GeoStatistics) + geo_statistics.init(c_geo_statistics, self) + return geo_statistics + @property def compression(self): """ diff --git a/python/pyarrow/parquet/core.py b/python/pyarrow/parquet/core.py --- a/python/pyarrow/parquet/core.py +++ b/python/pyarrow/parquet/core.py @@ -1270,9 +1270,9 @@ class ParquetDataset: page_checksum_verification : bool, default False If True, verify the page checksum for each page read from the file. arrow_extensions_enabled : bool, default False - If True, read Parquet logical types as Arrow extension types where possible, - (e.g., read JSON as the canonical `arrow.json` extension type or UUID as - the canonical `arrow.uuid` extension type). + If True, read Parquet logical types as Arrow extension types where possible, + (e.g., read JSON as the canonical `arrow.json` extension type or UUID as + the canonical `arrow.uuid` extension type). Examples -------- @@ -1287,7 +1287,6 @@ def __init__(self, path_or_paths, filesystem=None, schema=None, *, filters=None, thrift_container_size_limit=None, page_checksum_verification=False, arrow_extensions_enabled=False): - import pyarrow.dataset as ds # map format arguments @@ -1686,9 +1685,9 @@ def partitioning(self): page_checksum_verification : bool, default False If True, verify the checksum for each page read from the file. arrow_extensions_enabled : bool, default False - If True, read Parquet logical types as Arrow extension types where possible, - (e.g., read JSON as the canonical `arrow.json` extension type or UUID as - the canonical `arrow.uuid` extension type). + If True, read Parquet logical types as Arrow extension types where possible, + (e.g., read JSON as the canonical `arrow.json` extension type or UUID as + the canonical `arrow.uuid` extension type). Returns -------