paleolimbot commented on code in PR #45459: URL: https://github.com/apache/arrow/pull/45459#discussion_r2059542185
########## cpp/src/parquet/geospatial/statistics.cc: ########## @@ -0,0 +1,392 @@ +// 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. + +#include "parquet/geospatial/statistics.h" + +#include <cmath> +#include <memory> +#include <optional> + +#include "arrow/array.h" +#include "arrow/type.h" +#include "arrow/util/bit_run_reader.h" +#include "parquet/exception.h" +#include "parquet/geospatial/util_internal.h" + +using arrow::util::SafeLoad; + +namespace parquet::geospatial { + +class GeoStatisticsImpl { + public: + bool Equals(const GeoStatisticsImpl& other) const { + return is_valid_ == other.is_valid_ && + bounder_.GeometryTypes() == other.bounder_.GeometryTypes() && + bounder_.Bounds() == other.bounder_.Bounds(); + } + + void Merge(const GeoStatisticsImpl& other) { + is_valid_ = is_valid_ && other.is_valid_; + if (!is_valid_) { + return; + } + + if (is_wraparound_x() || other.is_wraparound_x()) { + throw ParquetException("Wraparound X is not supported by GeoStatistics::Merge()"); + } + + bounder_.MergeBox(other.bounder_.Bounds()); + std::vector<int32_t> other_geometry_types = other.bounder_.GeometryTypes(); + bounder_.MergeGeometryTypes(other_geometry_types); + } + + void Update(const ByteArray* values, int64_t num_values) { + if (!is_valid_) { + return; + } + + if (is_wraparound_x()) { + throw ParquetException("Wraparound X is not suppored by GeoStatistics::Update()"); + } + + for (int64_t i = 0; i < num_values; i++) { + const ByteArray& item = values[i]; + try { + bounder_.MergeGeometry({reinterpret_cast<const char*>(item.ptr), item.len}); + } catch (ParquetException&) { + is_valid_ = false; + return; + } + } + } + + void UpdateSpaced(const ByteArray* values, const uint8_t* valid_bits, + int64_t valid_bits_offset, int64_t num_spaced_values, + int64_t num_values) { + DCHECK_GT(num_spaced_values, 0); + + if (!is_valid_) { + return; + } + + if (is_wraparound_x()) { + throw ParquetException("Wraparound X is not suppored by GeoStatistics::Update()"); + } + + ::arrow::Status status = ::arrow::internal::VisitSetBitRuns( + valid_bits, valid_bits_offset, num_spaced_values, + [&](int64_t position, int64_t length) { + for (int64_t i = 0; i < length; i++) { + ByteArray item = SafeLoad(values + i + position); + PARQUET_CATCH_NOT_OK(bounder_.MergeGeometry( + {reinterpret_cast<const char*>(item.ptr), item.len})); + } + + return ::arrow::Status::OK(); + }); + + if (!status.ok()) { + is_valid_ = false; + } + } + + void Update(const ::arrow::Array& values) { + if (!is_valid_) { + return; + } + + if (is_wraparound_x()) { + throw ParquetException("Wraparound X is not suppored by GeoStatistics::Update()"); + } + + // Note that ::arrow::Type::EXTENSION seems to be handled before this is called + switch (values.type_id()) { + case ::arrow::Type::BINARY: + UpdateArrayImpl<::arrow::BinaryArray>(values); + break; + case ::arrow::Type::LARGE_BINARY: + UpdateArrayImpl<::arrow::LargeBinaryArray>(values); + break; + // This does not currently handle run-end encoded, dictionary encodings, or views + default: + throw ParquetException("Unsupported Array type in GeoStatistics::Update(Array): ", + values.type()->ToString()); + } + } + + void Reset() { + bounder_.Reset(); + is_valid_ = true; + } + + EncodedGeoStatistics Encode() const { + if (!is_valid_) { + return {}; + } + + const geospatial::BoundingBox::XYZM& mins = bounder_.Bounds().min; + const geospatial::BoundingBox::XYZM& maxes = bounder_.Bounds().max; + + EncodedGeoStatistics out; + + if (has_geometry_types_) { + out.geospatial_types = bounder_.GeometryTypes(); + } + + bool write_x = !bound_empty(0) && bound_valid(0); + bool write_y = !bound_empty(1) && bound_valid(1); + bool write_z = write_x && write_y && !bound_empty(2) && bound_valid(2); + bool write_m = write_x && write_y && !bound_empty(3) && bound_valid(3); + + if (write_x && write_y) { + out.xmin = mins[0]; + out.xmax = maxes[0]; + out.ymin = mins[1]; + out.ymax = maxes[1]; + out.xy_bounds_present = true; + } + + if (write_z) { + out.zmin = mins[2]; + out.zmax = maxes[2]; + out.z_bounds_present = true; + } + + if (write_m) { + out.mmin = mins[3]; + out.mmax = maxes[3]; + out.m_bounds_present = true; + } + + return out; + } + + void Update(const EncodedGeoStatistics& encoded) { + if (!is_valid_) { + return; + } + + // We can create GeoStatistics from a wraparound bounding box, but we can't + // update an existing one because the merge logic is not yet implemented. Review Comment: It is probably not hard but I'd rather not implement it in this PR since there's quite a bit here. This particular line it turns out was never called on a non-empty value, so I made that explicit (by renaming this to Decode() and moving the Reset() call and removing this check). The other place that threw for a wraparound I changed to mark the X values as invalid instead of throw. There aren't any writers writing these boxes yet. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
