paleolimbot commented on code in PR #45459: URL: https://github.com/apache/arrow/pull/45459#discussion_r2059270033
########## cpp/src/parquet/geospatial/util_internal.cc: ########## @@ -0,0 +1,238 @@ +// 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/util_internal.h" + +#include <sstream> + +#include "arrow/util/endian.h" +#include "arrow/util/macros.h" +#include "arrow/util/ubsan.h" +#include "parquet/exception.h" + +namespace parquet::geospatial { + +std::string BoundingBox::ToString() const { + std::stringstream ss; + ss << "BoundingBox" << std::endl; + ss << " x: [" << min[0] << ", " << max[0] << "]" << std::endl; + ss << " y: [" << min[1] << ", " << max[1] << "]" << std::endl; + ss << " z: [" << min[2] << ", " << max[2] << "]" << std::endl; + ss << " m: [" << min[3] << ", " << max[3] << "]" << std::endl; + + return ss.str(); +} + +/// \brief Object to keep track of the low-level consumption of a well-known binary +/// geometry +/// +/// Briefly, ISO well-known binary supported by the Parquet spec is an endian byte +/// (0x01 or 0x00), followed by geometry type + dimensions encoded as a (uint32_t), +/// followed by geometry-specific data. Coordinate sequences are represented by a +/// uint32_t (the number of coordinates) plus a sequence of doubles (number of coordinates +/// multiplied by the number of dimensions). +class WKBBuffer { + public: + WKBBuffer() : data_(nullptr), size_(0) {} + WKBBuffer(const uint8_t* data, int64_t size) : data_(data), size_(size) {} + + uint8_t ReadUInt8() { return ReadChecked<uint8_t>(); } + + uint32_t ReadUInt32(bool swap) { + auto value = ReadChecked<uint32_t>(); + if (swap) { + return ::arrow::bit_util::ByteSwap(value); + } else { + return value; + } + } + + template <typename Coord, typename Visit> + void ReadDoubles(uint32_t n_coords, bool swap, Visit&& visit) { + size_t total_bytes = n_coords * sizeof(Coord); + if (size_ < total_bytes) { + throw ParquetException("Can't read coordinate sequence of ", total_bytes, + " bytes from WKBBuffer with ", size_, " remaining"); + } + + if (swap) { + Coord coord; + for (uint32_t i = 0; i < n_coords; i++) { + coord = ReadUnchecked<Coord>(); + for (auto& c : coord) { + c = ::arrow::bit_util::ByteSwap(c); + } + + visit(coord); + } + } else { + for (uint32_t i = 0; i < n_coords; i++) { + visit(ReadUnchecked<Coord>()); + } + } + } + + size_t size() { return size_; } + + private: + const uint8_t* data_; + size_t size_; Review Comment: I need to be able to update `data_` and `size_` as we read values from the buffer. It didn't seem like the span was built for this but I'm happy to give it a try if it was! -- 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