|
35 | 35 |
|
36 | 36 | namespace iceberg { |
37 | 37 |
|
| 38 | +namespace { |
| 39 | + |
| 40 | +// A RAII guard for ArrowSchema. |
| 41 | +struct ArrowSchemaGuard { |
| 42 | + ArrowSchema* schema; |
| 43 | + ~ArrowSchemaGuard() { |
| 44 | + if (schema != nullptr && schema->release != nullptr) { |
| 45 | + schema->release(schema); |
| 46 | + } |
| 47 | + } |
| 48 | +}; |
| 49 | + |
| 50 | +} // namespace |
| 51 | + |
38 | 52 | TEST(ArrowCDataTest, CheckArrowSchemaAndArrayByNanoarrow) { |
39 | 53 | auto [schema, array] = internal::CreateExampleArrowSchemaAndArrayByNanoarrow(); |
40 | 54 |
|
@@ -86,7 +100,8 @@ TEST_P(ToArrowSchemaTest, PrimitiveType) { |
86 | 100 | : SchemaField::MakeRequired(kFieldId, std::string(kFieldName), |
87 | 101 | param.iceberg_type)}, |
88 | 102 | /*schema_id=*/0); |
89 | | - ArrowSchema arrow_schema; |
| 103 | + ArrowSchema arrow_schema{}; |
| 104 | + ArrowSchemaGuard guard{&arrow_schema}; |
90 | 105 | ASSERT_THAT(ToArrowSchema(schema, &arrow_schema), IsOk()); |
91 | 106 |
|
92 | 107 | auto imported_schema = ::arrow::ImportSchema(&arrow_schema).ValueOrDie(); |
@@ -170,7 +185,8 @@ TEST(ToArrowSchemaTest, StructType) { |
170 | 185 | struct_type)}, |
171 | 186 | /*schema_id=*/0); |
172 | 187 |
|
173 | | - ArrowSchema arrow_schema; |
| 188 | + ArrowSchema arrow_schema{}; |
| 189 | + ArrowSchemaGuard guard{&arrow_schema}; |
174 | 190 | ASSERT_THAT(ToArrowSchema(schema, &arrow_schema), IsOk()); |
175 | 191 |
|
176 | 192 | auto imported_schema = ::arrow::ImportSchema(&arrow_schema).ValueOrDie(); |
@@ -202,7 +218,8 @@ TEST(ToArrowSchemaTest, ListType) { |
202 | 218 | {SchemaField::MakeRequired(kListFieldId, std::string(kListFieldName), list_type)}, |
203 | 219 | /*schema_id=*/0); |
204 | 220 |
|
205 | | - ArrowSchema arrow_schema; |
| 221 | + ArrowSchema arrow_schema{}; |
| 222 | + ArrowSchemaGuard guard{&arrow_schema}; |
206 | 223 | ASSERT_THAT(ToArrowSchema(schema, &arrow_schema), IsOk()); |
207 | 224 |
|
208 | 225 | auto imported_schema = ::arrow::ImportSchema(&arrow_schema).ValueOrDie(); |
@@ -237,7 +254,8 @@ TEST(ToArrowSchemaTest, MapType) { |
237 | 254 | {SchemaField::MakeRequired(kFieldId, std::string(kMapFieldName), map_type)}, |
238 | 255 | /*schema_id=*/0); |
239 | 256 |
|
240 | | - ArrowSchema arrow_schema; |
| 257 | + ArrowSchema arrow_schema{}; |
| 258 | + ArrowSchemaGuard guard{&arrow_schema}; |
241 | 259 | ASSERT_THAT(ToArrowSchema(schema, &arrow_schema), IsOk()); |
242 | 260 |
|
243 | 261 | auto imported_schema = ::arrow::ImportSchema(&arrow_schema).ValueOrDie(); |
@@ -278,7 +296,8 @@ TEST_P(FromArrowSchemaTest, PrimitiveType) { |
278 | 296 | {std::string(kFieldIdKey), std::to_string(kFieldId)}}); |
279 | 297 | auto arrow_schema = ::arrow::schema({::arrow::field( |
280 | 298 | std::string(kFieldName), param.arrow_type, param.optional, std::move(metadata))}); |
281 | | - ArrowSchema exported_schema; |
| 299 | + ArrowSchema exported_schema{}; |
| 300 | + ArrowSchemaGuard guard{&exported_schema}; |
282 | 301 | ASSERT_TRUE(::arrow::ExportSchema(*arrow_schema, &exported_schema).ok()); |
283 | 302 |
|
284 | 303 | auto type_result = FromArrowSchema(exported_schema, /*schema_id=*/1); |
@@ -353,7 +372,8 @@ TEST(FromArrowSchemaTest, StructType) { |
353 | 372 | ::arrow::key_value_metadata(std::unordered_map<std::string, std::string>{ |
354 | 373 | {std::string(kFieldIdKey), std::to_string(kStructFieldId)}})); |
355 | 374 | auto arrow_schema = ::arrow::schema({struct_field}); |
356 | | - ArrowSchema exported_schema; |
| 375 | + ArrowSchema exported_schema{}; |
| 376 | + ArrowSchemaGuard guard{&exported_schema}; |
357 | 377 | ASSERT_TRUE(::arrow::ExportSchema(*arrow_schema, &exported_schema).ok()); |
358 | 378 |
|
359 | 379 | auto schema_result = FromArrowSchema(exported_schema, /*schema_id=*/0); |
@@ -403,7 +423,8 @@ TEST(FromArrowSchemaTest, ListType) { |
403 | 423 | {std::string(kFieldIdKey), std::to_string(kListFieldId)}})); |
404 | 424 | auto arrow_schema = ::arrow::schema({list_field}); |
405 | 425 |
|
406 | | - ArrowSchema exported_schema; |
| 426 | + ArrowSchema exported_schema{}; |
| 427 | + ArrowSchemaGuard guard{&exported_schema}; |
407 | 428 | ASSERT_TRUE(::arrow::ExportSchema(*arrow_schema, &exported_schema).ok()); |
408 | 429 |
|
409 | 430 | auto schema_result = FromArrowSchema(exported_schema, /*schema_id=*/0); |
@@ -453,7 +474,8 @@ TEST(FromArrowSchemaTest, MapType) { |
453 | 474 | {std::string(kFieldIdKey), std::to_string(kFieldId)}})); |
454 | 475 | auto arrow_schema = ::arrow::schema({map_field}); |
455 | 476 |
|
456 | | - ArrowSchema exported_schema; |
| 477 | + ArrowSchema exported_schema{}; |
| 478 | + ArrowSchemaGuard guard{&exported_schema}; |
457 | 479 | ASSERT_TRUE(::arrow::ExportSchema(*arrow_schema, &exported_schema).ok()); |
458 | 480 |
|
459 | 481 | auto schema_result = FromArrowSchema(exported_schema, /*schema_id=*/0); |
|
0 commit comments