paleolimbot commented on code in PR #45459: URL: https://github.com/apache/arrow/pull/45459#discussion_r2059542503
########## cpp/src/parquet/geospatial/statistics_test.cc: ########## @@ -0,0 +1,339 @@ +// 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 <gmock/gmock.h> +#include <gtest/gtest.h> + +#include "arrow/array/builder_binary.h" +#include "arrow/compute/api.h" +#include "arrow/testing/gtest_util.h" + +#include "parquet/geospatial/statistics.h" +#include "parquet/test_util.h" + +static constexpr double kInf = std::numeric_limits<double>::infinity(); + +namespace parquet::geospatial { + +TEST(TestGeoStatistics, TestDefaults) { + GeoStatistics stats; + EXPECT_TRUE(stats.geometry_types().has_value()); + EXPECT_EQ(stats.geometry_types().value().size(), 0); + EXPECT_TRUE(stats.is_valid()); + EXPECT_THAT(stats.dimension_empty(), ::testing::ElementsAre(true, true, true, true)); + EXPECT_THAT(stats.dimension_valid(), ::testing::ElementsAre(true, true, true, true)); + for (int i = 0; i < kMaxDimensions; i++) { + EXPECT_EQ(stats.lower_bound()[i], kInf); + EXPECT_EQ(stats.upper_bound()[i], -kInf); + } + + EXPECT_TRUE(stats.Equals(GeoStatistics())); Review Comment: I added a dedicated test explicitly setting various components to be unequal! ########## cpp/src/parquet/reader_test.cc: ########## @@ -1857,4 +1863,174 @@ TEST(PageIndexReaderTest, ReadFileWithoutPageIndex) { ASSERT_EQ(nullptr, row_group_index_reader); } +class TestGeometryLogicalType : public ::testing::Test { + public: + const int kNumRows = 1000; + + void WriteTestData(std::shared_ptr<const LogicalType> type, bool write_arrow) { + // Make schema + schema::NodeVector fields; + fields.push_back( + PrimitiveNode::Make("g", Repetition::REQUIRED, type, Type::BYTE_ARRAY)); + auto schema = std::static_pointer_cast<GroupNode>( + GroupNode::Make("schema", Repetition::REQUIRED, fields)); + + // Write small batches and small data pages + auto writer_props_builder = WriterProperties::Builder(); + writer_props_builder.write_batch_size(64); + + std::shared_ptr<WriterProperties> writer_props = writer_props_builder.build(); + + ASSERT_OK_AND_ASSIGN(auto out_file, ::arrow::io::BufferOutputStream::Create()); + std::shared_ptr<ParquetFileWriter> file_writer = + ParquetFileWriter::Open(out_file, schema, writer_props); + RowGroupWriter* rg_writer = file_writer->AppendRowGroup(); + + // write WKB points to columns + auto* writer = + ::arrow::internal::checked_cast<ByteArrayWriter*>(rg_writer->NextColumn()); + if (!write_arrow) { + WriteTestDataUsingWriteBatch(writer); + } else { + WriteTestDataUsingWriteArrow(writer); + } + + rg_writer->Close(); + file_writer->Close(); + + ASSERT_OK_AND_ASSIGN(file_buf, out_file->Finish()); + } + + void WriteTestDataUsingWriteBatch(ByteArrayWriter* writer) { + std::vector<uint8_t> buffer(test::kWkbPointXYSize * kNumRows); + uint8_t* ptr = buffer.data(); + std::vector<ByteArray> values(kNumRows); + for (int k = 0; k < kNumRows; k++) { + std::string item = test::MakeWKBPoint( + {static_cast<double>(k), static_cast<double>(k + 1)}, false, false); + std::memcpy(ptr, item.data(), item.size()); + values[k].len = test::kWkbPointXYSize; + values[k].ptr = ptr; + ptr += test::kWkbPointXYSize; + } + writer->WriteBatch(kNumRows, nullptr, nullptr, values.data()); + } + + void WriteTestDataUsingWriteArrow(ByteArrayWriter* writer) { + ::arrow::BinaryBuilder builder; + for (int k = 0; k < kNumRows; k++) { + std::string item = test::MakeWKBPoint( + {static_cast<double>(k), static_cast<double>(k + 1)}, false, false); + + ASSERT_OK(builder.Append(item)); + } + std::shared_ptr<::arrow::BinaryArray> array; + ASSERT_OK(builder.Finish(&array)); + + std::shared_ptr<ArrowWriterProperties> properties = + ArrowWriterProperties::Builder().build(); + MemoryPool* pool = ::arrow::default_memory_pool(); + auto ctx = std::make_unique<ArrowWriteContext>(pool, properties.get()); + ASSERT_OK(writer->WriteArrow(nullptr, nullptr, kNumRows, *array, ctx.get(), + /*leaf_field_nullable=*/true)); + } + + void TestWriteAndRead(std::shared_ptr<const LogicalType> type, bool write_arrow) { + ASSERT_NO_FATAL_FAILURE(WriteTestData(type, write_arrow)); + + auto in_file = std::make_shared<::arrow::io::BufferReader>(file_buf); + + ReaderProperties reader_props; + reader_props.enable_buffered_stream(); + reader_props.set_buffer_size(64); + auto file_reader = ParquetFileReader::Open(in_file, reader_props); + + // Check that the geometry statistics are correctly written and read + auto metadata = file_reader->metadata(); + ASSERT_TRUE(type->Equals(*metadata->schema()->Column(0)->logical_type())); + + auto page_index_reader = file_reader->GetPageIndexReader(); + int num_row_groups = metadata->num_row_groups(); + int64_t start_index = 0; + for (int i = 0; i < num_row_groups; i++) { + auto row_group_metadata = metadata->RowGroup(i); + auto column_chunk_metadata = row_group_metadata->ColumnChunk(0); + auto geo_stats = column_chunk_metadata->geo_statistics(); + ASSERT_NO_FATAL_FAILURE(CheckGeoStatistics(type, geo_stats, start_index, + row_group_metadata->num_rows())); + start_index += row_group_metadata->num_rows(); + } + + // Check the geometry values + int64_t total_values_read = 0; + for (int i = 0; i < num_row_groups; i++) { + auto row_group = file_reader->RowGroup(i); + std::shared_ptr<ByteArrayReader> reader = + std::static_pointer_cast<ByteArrayReader>(row_group->Column(0)); + while (reader->HasNext()) { + std::vector<ByteArray> out(kNumRows); + int64_t values_read = 0; + int64_t levels_read = + reader->ReadBatch(kNumRows, nullptr, nullptr, out.data(), &values_read); + ASSERT_GE(levels_read, 1); + ASSERT_GE(values_read, 1); + + // Check the batch + for (int64_t i = 0; i < values_read; i++) { + const ByteArray& value = out[i]; + auto xy = test::GetWKBPointCoordinateXY(value); + EXPECT_TRUE(xy.has_value()); + auto expected_x = static_cast<double>(i + total_values_read); + auto expected_y = static_cast<double>(i + 1 + total_values_read); + EXPECT_EQ(*xy, (std::pair<double, double>(expected_x, expected_y))); + } + + total_values_read += values_read; + } + } + EXPECT_EQ(kNumRows, total_values_read); + } + + void CheckGeoStatistics(std::shared_ptr<const LogicalType> type, + std::shared_ptr<geospatial::GeoStatistics> geom_stats, + int64_t start_index, int64_t num_rows) { + // We don't yet generate statistics for Geography + if (type->is_geography()) { + ASSERT_EQ(geom_stats, nullptr); + return; + } + + ASSERT_NE(geom_stats, nullptr); + // We wrote exactly one geometry type (POINT, which has code 1) + EXPECT_THAT(*geom_stats->geometry_types(), ::testing::ElementsAre(1)); + + double expected_xmin = static_cast<double>(start_index); + double expected_xmax = expected_xmin + num_rows - 1; + double expected_ymin = expected_xmin + 1; + double expected_ymax = expected_xmax + 1; + + EXPECT_EQ(geom_stats->lower_bound()[0], expected_xmin); + EXPECT_EQ(geom_stats->upper_bound()[0], expected_xmax); + EXPECT_EQ(geom_stats->lower_bound()[1], expected_ymin); + EXPECT_EQ(geom_stats->upper_bound()[1], expected_ymax); + EXPECT_THAT(geom_stats->dimension_valid(), + ::testing::ElementsAre(true, true, false, false)); + } + + protected: + std::shared_ptr<Buffer> file_buf; +}; + +TEST_F(TestGeometryLogicalType, TestWriteGeometry) { + TestWriteAndRead(GeometryLogicalType::Make("srid:1234"), /*write_arrow=*/false); +} + +TEST_F(TestGeometryLogicalType, TestWriteArrowAndReadGeometry) { + TestWriteAndRead(GeometryLogicalType::Make("srid:1234"), /*write_arrow=*/true); +} + +TEST_F(TestGeometryLogicalType, TestWriteGeography) { + TestWriteAndRead(GeographyLogicalType::Make("srid:1234"), /*write_arrow=*/false); Review Comment: Added! -- 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