paleolimbot commented on code in PR #45459: URL: https://github.com/apache/arrow/pull/45459#discussion_r1975790699
########## cpp/src/parquet/geometry_statistics.cc: ########## @@ -0,0 +1,291 @@ +// 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/geometry_statistics.h" +#include <memory> + +#include "arrow/array.h" +#include "arrow/type.h" +#include "arrow/util/bit_run_reader.h" +#include "arrow/util/logging.h" +#include "parquet/exception.h" +#include "parquet/geometry_util_internal.h" + +using arrow::util::SafeLoad; + +namespace parquet { + +class GeospatialStatisticsImpl { + public: + GeospatialStatisticsImpl() = default; + GeospatialStatisticsImpl(const GeospatialStatisticsImpl&) = default; + + bool Equals(const GeospatialStatisticsImpl& other) const { + if (is_valid_ != other.is_valid_) { + return false; + } + + if (!is_valid_ && !other.is_valid_) { + return true; + } + + auto geospatial_types = bounder_.GeometryTypes(); + auto other_geospatial_types = other.bounder_.GeometryTypes(); + if (geospatial_types.size() != other_geospatial_types.size()) { + return false; + } + + for (size_t i = 0; i < geospatial_types.size(); i++) { + if (geospatial_types[i] != other_geospatial_types[i]) { + return false; + } + } + + return bounder_.Bounds() == other.bounder_.Bounds(); + } + + void Merge(const GeospatialStatisticsImpl& other) { + is_valid_ = is_valid_ && other.is_valid_; + bounder_.ReadBox(other.bounder_.Bounds()); + bounder_.ReadGeometryTypes(other.bounder_.GeometryTypes()); + } + + void Update(const ByteArray* values, int64_t num_values, int64_t null_count) { + if (!is_valid_) { + return; + } + + for (int64_t i = 0; i < num_values; i++) { + const ByteArray& item = values[i]; + ::arrow::Status status = bounder_.ReadGeometry(item.ptr, item.len); + if (!status.ok()) { + 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, int64_t null_count) { + DCHECK_GT(num_spaced_values, 0); + + if (!is_valid_) { + return; + } + + ::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); + ARROW_RETURN_NOT_OK(bounder_.ReadGeometry(item.ptr, item.len)); + } + + return ::arrow::Status::OK(); + }); + + if (!status.ok()) { + is_valid_ = false; + } + } + + void Update(const ::arrow::Array& values) { + if (!is_valid_) { + return; + } + + // 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 GeospatialStatistics::Update(Array): ", + values.type()->ToString()); + } + } + + void Reset() { + bounder_.Reset(); + is_valid_ = true; + } + + EncodedGeospatialStatistics Encode() const { + if (!is_valid_) { + return {}; + } + + const geometry::BoundingBox::XYZM& mins = bounder_.Bounds().min; + const geometry::BoundingBox::XYZM& maxes = bounder_.Bounds().max; + + EncodedGeospatialStatistics out; + out.geospatial_types = bounder_.GeometryTypes(); + + out.xmin = mins[0]; + out.xmax = maxes[0]; + out.ymin = mins[1]; + out.ymax = maxes[1]; + out.zmin = mins[2]; Review Comment: Good call! I now check for the existence of Z and M here. -- 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]
