|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +#include "gtest/gtest.h" |
| 19 | + |
| 20 | +#include "arrow/test-util.h" |
| 21 | +#include "arrow/parquet/reader.h" |
| 22 | +#include "arrow/types/primitive.h" |
| 23 | +#include "arrow/util/memory-pool.h" |
| 24 | +#include "arrow/util/status.h" |
| 25 | + |
| 26 | +#include "parquet/api/schema.h" |
| 27 | +#include "parquet/column/writer.h" |
| 28 | +#include "parquet/file/reader.h" |
| 29 | +#include "parquet/file/writer.h" |
| 30 | +#include "parquet/util/input.h" |
| 31 | +#include "parquet/util/output.h" |
| 32 | + |
| 33 | +using ParquetBuffer = parquet::Buffer; |
| 34 | +using parquet::BufferReader; |
| 35 | +using parquet::InMemoryOutputStream; |
| 36 | +using parquet::Int64Writer; |
| 37 | +using parquet::ParquetFileReader; |
| 38 | +using parquet::ParquetFileWriter; |
| 39 | +using parquet::RandomAccessSource; |
| 40 | +using parquet::Repetition; |
| 41 | +using parquet::SchemaDescriptor; |
| 42 | +using ParquetType = parquet::Type; |
| 43 | +using parquet::schema::GroupNode; |
| 44 | +using parquet::schema::NodePtr; |
| 45 | +using parquet::schema::PrimitiveNode; |
| 46 | + |
| 47 | +namespace arrow { |
| 48 | + |
| 49 | +namespace parquet { |
| 50 | + |
| 51 | +class TestReadParquet : public ::testing::Test { |
| 52 | + public: |
| 53 | + virtual void SetUp() {} |
| 54 | + |
| 55 | + std::shared_ptr<GroupNode> Int64Schema() { |
| 56 | + auto pnode = PrimitiveNode::Make("int64", Repetition::REQUIRED, ParquetType::INT64); |
| 57 | + NodePtr node_ = |
| 58 | + GroupNode::Make("schema", Repetition::REQUIRED, std::vector<NodePtr>({pnode})); |
| 59 | + return std::static_pointer_cast<GroupNode>(node_); |
| 60 | + } |
| 61 | + |
| 62 | + std::unique_ptr<ParquetFileReader> Int64File( |
| 63 | + std::vector<int64_t>& values, int num_chunks) { |
| 64 | + std::shared_ptr<GroupNode> schema = Int64Schema(); |
| 65 | + std::shared_ptr<InMemoryOutputStream> sink(new InMemoryOutputStream()); |
| 66 | + auto file_writer = ParquetFileWriter::Open(sink, schema); |
| 67 | + size_t chunk_size = values.size() / num_chunks; |
| 68 | + for (int i = 0; i < num_chunks; i++) { |
| 69 | + auto row_group_writer = file_writer->AppendRowGroup(chunk_size); |
| 70 | + auto column_writer = static_cast<Int64Writer*>(row_group_writer->NextColumn()); |
| 71 | + int64_t* data = values.data() + i * chunk_size; |
| 72 | + column_writer->WriteBatch(chunk_size, nullptr, nullptr, data); |
| 73 | + column_writer->Close(); |
| 74 | + row_group_writer->Close(); |
| 75 | + } |
| 76 | + file_writer->Close(); |
| 77 | + |
| 78 | + std::shared_ptr<ParquetBuffer> buffer = sink->GetBuffer(); |
| 79 | + std::unique_ptr<RandomAccessSource> source(new BufferReader(buffer)); |
| 80 | + return ParquetFileReader::Open(std::move(source)); |
| 81 | + } |
| 82 | + |
| 83 | + private: |
| 84 | +}; |
| 85 | + |
| 86 | +TEST_F(TestReadParquet, SingleColumnInt64) { |
| 87 | + std::vector<int64_t> values(100, 128); |
| 88 | + std::unique_ptr<ParquetFileReader> file_reader = Int64File(values, 1); |
| 89 | + arrow::parquet::FileReader reader(default_memory_pool(), std::move(file_reader)); |
| 90 | + std::unique_ptr<arrow::parquet::FlatColumnReader> column_reader; |
| 91 | + ASSERT_OK(reader.GetFlatColumn(0, &column_reader)); |
| 92 | + ASSERT_NE(nullptr, column_reader.get()); |
| 93 | + std::shared_ptr<Array> out; |
| 94 | + ASSERT_OK(column_reader->NextBatch(100, &out)); |
| 95 | + ASSERT_NE(nullptr, out.get()); |
| 96 | + Int64Array* out_array = static_cast<Int64Array*>(out.get()); |
| 97 | + for (size_t i = 0; i < values.size(); i++) { |
| 98 | + EXPECT_EQ(values[i], out_array->raw_data()[i]); |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +TEST_F(TestReadParquet, SingleColumnInt64Chunked) { |
| 103 | + std::vector<int64_t> values(100, 128); |
| 104 | + std::unique_ptr<ParquetFileReader> file_reader = Int64File(values, 4); |
| 105 | + arrow::parquet::FileReader reader(default_memory_pool(), std::move(file_reader)); |
| 106 | + std::unique_ptr<arrow::parquet::FlatColumnReader> column_reader; |
| 107 | + ASSERT_OK(reader.GetFlatColumn(0, &column_reader)); |
| 108 | + ASSERT_NE(nullptr, column_reader.get()); |
| 109 | + std::shared_ptr<Array> out; |
| 110 | + ASSERT_OK(column_reader->NextBatch(100, &out)); |
| 111 | + ASSERT_NE(nullptr, out.get()); |
| 112 | + Int64Array* out_array = static_cast<Int64Array*>(out.get()); |
| 113 | + for (size_t i = 0; i < values.size(); i++) { |
| 114 | + EXPECT_EQ(values[i], out_array->raw_data()[i]); |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +} // namespace parquet |
| 119 | + |
| 120 | +} // namespace arrow |
0 commit comments