paleolimbot commented on code in PR #45459: URL: https://github.com/apache/arrow/pull/45459#discussion_r2050744275
########## cpp/src/parquet/geospatial/statistics.cc: ########## @@ -0,0 +1,393 @@ +// 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 = !bound_empty(2) && bound_valid(2); + bool write_m = !bound_empty(3) && bound_valid(3); Review Comment: I think functionally that happened (but it was tucked away in the thrift_internal.h). Your version is clearer! -- 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: github-unsubscr...@arrow.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org