|
29 | 29 | #include "iceberg/avro/avro_register.h" |
30 | 30 | #include "iceberg/data/equality_delete_writer.h" |
31 | 31 | #include "iceberg/data/position_delete_writer.h" |
| 32 | +#include "iceberg/expression/literal.h" |
32 | 33 | #include "iceberg/file_format.h" |
33 | 34 | #include "iceberg/file_reader.h" |
34 | 35 | #include "iceberg/manifest/manifest_entry.h" |
@@ -188,6 +189,44 @@ TEST_P(DataWriterFormatTest, WriteRowLineage) { |
188 | 189 | [3, 102, 7]])")); |
189 | 190 | } |
190 | 191 |
|
| 192 | +TEST_P(DataWriterFormatTest, WriteMissingColumnUsesWriteDefault) { |
| 193 | + auto [format, path] = GetParam(); |
| 194 | + auto input_schema = std::make_shared<Schema>( |
| 195 | + std::vector<SchemaField>{SchemaField::MakeRequired(1, "id", int32())}); |
| 196 | + auto write_schema = std::make_shared<Schema>(std::vector<SchemaField>{ |
| 197 | + SchemaField::MakeRequired(1, "id", int32()), |
| 198 | + SchemaField(2, "added", int32(), /*optional=*/false, /*doc=*/{}, |
| 199 | + std::make_shared<const Literal>(Literal::Int(42)), |
| 200 | + std::make_shared<const Literal>(Literal::Int(7))), |
| 201 | + }); |
| 202 | + DataWriterOptions options{ |
| 203 | + .path = path, |
| 204 | + .schema = write_schema, |
| 205 | + .input_schema = input_schema, |
| 206 | + .spec = partition_spec_, |
| 207 | + .partition = PartitionValues{}, |
| 208 | + .format = format, |
| 209 | + .io = file_io_, |
| 210 | + .properties = FormatProperties(format), |
| 211 | + }; |
| 212 | + |
| 213 | + ICEBERG_UNWRAP_OR_FAIL(auto writer, DataWriter::Make(options)); |
| 214 | + auto input = CreateArray(*input_schema, R"([[1], [2]])"); |
| 215 | + ArrowArray arrow_array; |
| 216 | + ASSERT_TRUE(::arrow::ExportArray(*input, &arrow_array).ok()); |
| 217 | + ASSERT_THAT(writer->Write(&arrow_array), IsOk()); |
| 218 | + ASSERT_THAT(writer->Close(), IsOk()); |
| 219 | + |
| 220 | + ICEBERG_UNWRAP_OR_FAIL( |
| 221 | + auto reader, |
| 222 | + ReaderFactoryRegistry::Open( |
| 223 | + format, {.path = path, .io = file_io_, .projection = write_schema})); |
| 224 | + |
| 225 | + // The file was written after the column was added, so the missing input column uses |
| 226 | + // write-default (7), not initial-default (42). |
| 227 | + ASSERT_NO_FATAL_FAILURE(VerifyNextBatch(*reader, *write_schema, R"([[1, 7], [2, 7]])")); |
| 228 | +} |
| 229 | + |
191 | 230 | INSTANTIATE_TEST_SUITE_P( |
192 | 231 | FormatTypes, DataWriterFormatTest, |
193 | 232 | ::testing::Values(std::make_pair(FileFormatType::kParquet, "test_data.parquet"), |
|
0 commit comments