davidnich opened a new pull request, #55635: URL: https://github.com/apache/arrow/pull/55635
### Rationale for this change Some IPC serialization functions take a `MemoryPool* pool` but build their `IpcWriteOptions` with the default pool, so the flatbuffer metadata is allocated from `default_memory_pool()` instead of the pool the caller gave. This defeats pool accounting and limits. Parquet hits it through `SerializeSchema()`, which `GetSchemaMetadata()` calls in `FileWriter::Open()` when `ArrowWriterProperties::store_schema()` is set. The affected functions are: - `ipc::SerializeSchema(schema, pool)`: `pool` was passed only to the output `BufferOutputStream`; the writer used `IpcWriteOptions::Defaults()`. - `ipc::GetTensorMessage(tensor, pool)`: `pool` was used only to make a non-contiguous tensor contiguous; the tensor message used a default-constructed `IpcWriteOptions`. - `ipc::GetSparseTensorPayload(sparse_tensor, pool, out)`, and so `ipc::GetSparseTensorMessage(sparse_tensor, pool)`: `pool` was not used; `SparseTensorSerializer` used `IpcWriteOptions::Defaults()`. ### What changes are included in this PR? - Set `IpcWriteOptions::memory_pool` to the given pool in `SerializeSchema()` and `GetTensorMessage()`. - Give the internal `SparseTensorSerializer` a pool argument. `GetSparseTensorPayload()` passes its `pool`, and `WriteSparseTensor()`, which takes no pool, passes `default_memory_pool()`, as before. ### Are these changes tested? Yes, with one new test per function: - `TestSchemaMetadata.SerializeSchemaUsesGivenMemoryPool` in `cpp/src/arrow/ipc/read_write_test.cc` - `TestTensor.GetTensorMessageUsesGivenMemoryPool` in `cpp/src/arrow/ipc/tensor_test.cc`, for a contiguous and a non-contiguous tensor - `TestSparseTensor.GetSparseTensorPayloadUsesGivenMemoryPool` in `cpp/src/arrow/ipc/tensor_test.cc`, for `GetSparseTensorPayload()` and `GetSparseTensorMessage()` Each test serializes with a pool from `MemoryPool::CreateDefault()`, which has its own statistics and so is distinct from `default_memory_pool()` whatever allocator backs both. It checks that `default_memory_pool()->total_bytes_allocated()` is unchanged, that the given pool was used, and that the result reads back equal (`ReadSchema()`, `ReadTensor()`, `internal::ReadSparseTensorPayload()`). For `GetSparseTensorMessage()`, the test compares the message metadata with the payload's. Each test fails without its fix and passes with it. ### Are there any user-facing changes? No API changes; these functions now allocate only from the pool they are given. ### Was AI used for this PR? In accordance to the [AI generation guidelines](https://arrow.apache.org/docs/dev/developers/overview.html#ai-generated-code), please disclose below whether and how AI was used in this PR. **PR code and description written by:** - [ ] Human - [x] AI **Reviewed before submission by:** - [x] Human - [ ] AI - [ ] Not reviewed * GitHub Issue: #55623 -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
