|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +#include <arrow/array.h> |
| 21 | +#include <arrow/c/bridge.h> |
| 22 | +#include <arrow/json/from_string.h> |
| 23 | +#include <arrow/record_batch.h> |
| 24 | +#include <arrow/table.h> |
| 25 | +#include <arrow/util/key_value_metadata.h> |
| 26 | +#include <parquet/arrow/reader.h> |
| 27 | +#include <parquet/arrow/writer.h> |
| 28 | +#include <parquet/metadata.h> |
| 29 | + |
| 30 | +#include "iceberg/arrow/arrow_fs_file_io_internal.h" |
| 31 | +#include "iceberg/file_format.h" |
| 32 | +#include "iceberg/manifest_entry.h" |
| 33 | +#include "iceberg/parquet/parquet_register.h" |
| 34 | +#include "iceberg/schema.h" |
| 35 | +#include "iceberg/table_scan.h" |
| 36 | +#include "iceberg/type.h" |
| 37 | +#include "iceberg/util/checked_cast.h" |
| 38 | +#include "matchers.h" |
| 39 | +#include "temp_file_test_base.h" |
| 40 | + |
| 41 | +namespace iceberg { |
| 42 | + |
| 43 | +class FileScanTaskTest : public TempFileTestBase { |
| 44 | + protected: |
| 45 | + static void SetUpTestSuite() { parquet::RegisterAll(); } |
| 46 | + |
| 47 | + void SetUp() override { |
| 48 | + TempFileTestBase::SetUp(); |
| 49 | + file_io_ = arrow::ArrowFileSystemFileIO::MakeLocalFileIO(); |
| 50 | + temp_parquet_file_ = CreateNewTempFilePathWithSuffix(".parquet"); |
| 51 | + CreateSimpleParquetFile(); |
| 52 | + } |
| 53 | + |
| 54 | + // Helper method to create a Parquet file with sample data. |
| 55 | + void CreateSimpleParquetFile(int64_t chunk_size = 1024) { |
| 56 | + const std::string kParquetFieldIdKey = "PARQUET:field_id"; |
| 57 | + auto arrow_schema = ::arrow::schema( |
| 58 | + {::arrow::field("id", ::arrow::int32(), /*nullable=*/false, |
| 59 | + ::arrow::KeyValueMetadata::Make({kParquetFieldIdKey}, {"1"})), |
| 60 | + ::arrow::field("name", ::arrow::utf8(), /*nullable=*/true, |
| 61 | + ::arrow::KeyValueMetadata::Make({kParquetFieldIdKey}, {"2"}))}); |
| 62 | + auto table = ::arrow::Table::FromRecordBatches( |
| 63 | + arrow_schema, {::arrow::RecordBatch::FromStructArray( |
| 64 | + ::arrow::json::ArrayFromJSONString( |
| 65 | + ::arrow::struct_(arrow_schema->fields()), |
| 66 | + R"([[1, "Foo"], [2, "Bar"], [3, "Baz"]])") |
| 67 | + .ValueOrDie()) |
| 68 | + .ValueOrDie()}) |
| 69 | + .ValueOrDie(); |
| 70 | + |
| 71 | + auto io = internal::checked_cast<arrow::ArrowFileSystemFileIO&>(*file_io_); |
| 72 | + auto outfile = io.fs()->OpenOutputStream(temp_parquet_file_).ValueOrDie(); |
| 73 | + |
| 74 | + ASSERT_TRUE(::parquet::arrow::WriteTable(*table, ::arrow::default_memory_pool(), |
| 75 | + outfile, chunk_size) |
| 76 | + .ok()); |
| 77 | + } |
| 78 | + |
| 79 | + // Helper to create a valid but empty Parquet file. |
| 80 | + void CreateEmptyParquetFile() { |
| 81 | + const std::string kParquetFieldIdKey = "PARQUET:field_id"; |
| 82 | + auto arrow_schema = ::arrow::schema( |
| 83 | + {::arrow::field("id", ::arrow::int32(), /*nullable=*/false, |
| 84 | + ::arrow::KeyValueMetadata::Make({kParquetFieldIdKey}, {"1"}))}); |
| 85 | + auto empty_table = ::arrow::Table::FromRecordBatches(arrow_schema, {}).ValueOrDie(); |
| 86 | + |
| 87 | + auto io = internal::checked_cast<arrow::ArrowFileSystemFileIO&>(*file_io_); |
| 88 | + auto outfile = io.fs()->OpenOutputStream(temp_parquet_file_).ValueOrDie(); |
| 89 | + ASSERT_TRUE(::parquet::arrow::WriteTable(*empty_table, ::arrow::default_memory_pool(), |
| 90 | + outfile, 1024) |
| 91 | + .ok()); |
| 92 | + } |
| 93 | + |
| 94 | + // Helper method to verify the content of the next batch from an ArrowArrayStream. |
| 95 | + void VerifyStreamNextBatch(struct ArrowArrayStream* stream, |
| 96 | + std::string_view expected_json) { |
| 97 | + auto record_batch_reader = ::arrow::ImportRecordBatchReader(stream).ValueOrDie(); |
| 98 | + |
| 99 | + auto result = record_batch_reader->Next(); |
| 100 | + ASSERT_TRUE(result.ok()) << result.status().message(); |
| 101 | + auto actual_batch = result.ValueOrDie(); |
| 102 | + ASSERT_NE(actual_batch, nullptr) << "Stream is exhausted but expected more data."; |
| 103 | + |
| 104 | + auto arrow_schema = actual_batch->schema(); |
| 105 | + auto struct_type = ::arrow::struct_(arrow_schema->fields()); |
| 106 | + auto expected_array = |
| 107 | + ::arrow::json::ArrayFromJSONString(struct_type, expected_json).ValueOrDie(); |
| 108 | + auto expected_batch = |
| 109 | + ::arrow::RecordBatch::FromStructArray(expected_array).ValueOrDie(); |
| 110 | + |
| 111 | + ASSERT_TRUE(actual_batch->Equals(*expected_batch)) |
| 112 | + << "Actual batch:\n" |
| 113 | + << actual_batch->ToString() << "\nExpected batch:\n" |
| 114 | + << expected_batch->ToString(); |
| 115 | + } |
| 116 | + |
| 117 | + // Helper method to verify that an ArrowArrayStream is exhausted. |
| 118 | + void VerifyStreamExhausted(struct ArrowArrayStream* stream) { |
| 119 | + auto record_batch_reader = ::arrow::ImportRecordBatchReader(stream).ValueOrDie(); |
| 120 | + auto result = record_batch_reader->Next(); |
| 121 | + ASSERT_TRUE(result.ok()) << result.status().message(); |
| 122 | + ASSERT_EQ(result.ValueOrDie(), nullptr) << "Reader was not exhausted as expected."; |
| 123 | + } |
| 124 | + |
| 125 | + std::shared_ptr<FileIO> file_io_; |
| 126 | + std::string temp_parquet_file_; |
| 127 | +}; |
| 128 | + |
| 129 | +TEST_F(FileScanTaskTest, ReadFullSchema) { |
| 130 | + auto data_file = std::make_shared<DataFile>(); |
| 131 | + data_file->file_path = temp_parquet_file_; |
| 132 | + data_file->file_format = FileFormatType::kParquet; |
| 133 | + |
| 134 | + auto projected_schema = std::make_shared<Schema>( |
| 135 | + std::vector<SchemaField>{SchemaField::MakeRequired(1, "id", int32()), |
| 136 | + SchemaField::MakeOptional(2, "name", string())}); |
| 137 | + |
| 138 | + FileScanTask task(data_file); |
| 139 | + |
| 140 | + auto stream_result = task.ToArrow(file_io_, projected_schema, nullptr); |
| 141 | + ASSERT_THAT(stream_result, IsOk()); |
| 142 | + auto stream = std::move(stream_result.value()); |
| 143 | + |
| 144 | + ASSERT_NO_FATAL_FAILURE( |
| 145 | + VerifyStreamNextBatch(&stream, R"([[1, "Foo"], [2, "Bar"], [3, "Baz"]])")); |
| 146 | +} |
| 147 | + |
| 148 | +TEST_F(FileScanTaskTest, ReadProjectedAndReorderedSchema) { |
| 149 | + auto data_file = std::make_shared<DataFile>(); |
| 150 | + data_file->file_path = temp_parquet_file_; |
| 151 | + data_file->file_format = FileFormatType::kParquet; |
| 152 | + |
| 153 | + auto projected_schema = std::make_shared<Schema>( |
| 154 | + std::vector<SchemaField>{SchemaField::MakeOptional(2, "name", string()), |
| 155 | + SchemaField::MakeOptional(3, "score", float64())}); |
| 156 | + |
| 157 | + FileScanTask task(data_file); |
| 158 | + |
| 159 | + auto stream_result = task.ToArrow(file_io_, projected_schema, nullptr); |
| 160 | + ASSERT_THAT(stream_result, IsOk()); |
| 161 | + auto stream = std::move(stream_result.value()); |
| 162 | + |
| 163 | + ASSERT_NO_FATAL_FAILURE( |
| 164 | + VerifyStreamNextBatch(&stream, R"([["Foo", null], ["Bar", null], ["Baz", null]])")); |
| 165 | +} |
| 166 | + |
| 167 | +TEST_F(FileScanTaskTest, ReadEmptyFile) { |
| 168 | + CreateEmptyParquetFile(); |
| 169 | + auto data_file = std::make_shared<DataFile>(); |
| 170 | + data_file->file_path = temp_parquet_file_; |
| 171 | + data_file->file_format = FileFormatType::kParquet; |
| 172 | + |
| 173 | + auto projected_schema = std::make_shared<Schema>( |
| 174 | + std::vector<SchemaField>{SchemaField::MakeRequired(1, "id", int32())}); |
| 175 | + |
| 176 | + FileScanTask task(data_file); |
| 177 | + |
| 178 | + auto stream_result = task.ToArrow(file_io_, projected_schema, nullptr); |
| 179 | + ASSERT_THAT(stream_result, IsOk()); |
| 180 | + auto stream = std::move(stream_result.value()); |
| 181 | + |
| 182 | + // The stream should be immediately exhausted |
| 183 | + ASSERT_NO_FATAL_FAILURE(VerifyStreamExhausted(&stream)); |
| 184 | +} |
| 185 | + |
| 186 | +} // namespace iceberg |
0 commit comments