fix(arrow): preserve allocator exception details in OOM statuses (#605)
diff --git a/src/paimon/common/utils/arrow/mem_utils.cpp b/src/paimon/common/utils/arrow/mem_utils.cpp
index 7e97d8c..625f5d3 100644
--- a/src/paimon/common/utils/arrow/mem_utils.cpp
+++ b/src/paimon/common/utils/arrow/mem_utils.cpp
@@ -68,8 +68,9 @@
         uint8_t* new_out = nullptr;
         try {
             new_out = reinterpret_cast<uint8_t*>(pool_.Malloc(size, alignment));
-        } catch (const std::bad_alloc&) {
-            return arrow::Status::OutOfMemory(fmt::format("failed to allocate {} bytes", size));
+        } catch (const std::bad_alloc& e) {
+            return arrow::Status::OutOfMemory(
+                fmt::format("failed to allocate {} bytes: {}", size, e.what()));
         }
         if (size > 0 && new_out == nullptr) {
             return arrow::Status::OutOfMemory(fmt::format("failed to allocate {} bytes", size));
@@ -85,9 +86,10 @@
         try {
             new_ptr =
                 reinterpret_cast<uint8_t*>(pool_.Realloc(*ptr, old_size, new_size, alignment));
-        } catch (const std::bad_alloc&) {
+        } catch (const std::bad_alloc& e) {
             return arrow::Status::OutOfMemory(
-                fmt::format("failed to reallocate memory from {} to {} bytes", old_size, new_size));
+                fmt::format("failed to reallocate memory from {} to {} bytes: {}", old_size,
+                            new_size, e.what()));
         }
         if (new_size > 0 && new_ptr == nullptr) {
             return arrow::Status::OutOfMemory(
diff --git a/src/paimon/common/utils/arrow/mem_utils_test.cpp b/src/paimon/common/utils/arrow/mem_utils_test.cpp
index 30c7afd..9e6d494 100644
--- a/src/paimon/common/utils/arrow/mem_utils_test.cpp
+++ b/src/paimon/common/utils/arrow/mem_utils_test.cpp
@@ -22,6 +22,7 @@
 #include <cstdint>
 #include <memory>
 #include <new>
+#include <utility>
 #include <vector>
 
 #include "arrow/api.h"
@@ -36,39 +37,57 @@
 namespace paimon::test {
 namespace {
 
-// A MemoryPool whose allocations always fail, either by returning nullptr or by
-// throwing std::bad_alloc, so the adaptor's out-of-memory paths can be exercised.
-class FailingMemoryPool : public MemoryPool {
+class MemoryBudgetExceeded : public std::bad_alloc {
  public:
-    enum class Mode { kReturnNull, kThrowBadAlloc };
+    const char* what() const noexcept override {
+        return "test memory budget exhausted";
+    }
+};
 
-    explicit FailingMemoryPool(Mode mode) : mode_(mode) {}
+class SizeLimitedMemoryPool : public MemoryPool {
+ public:
+    enum class FailureMode { kReturnNull, kThrowBadAlloc };
 
-    void* Malloc(uint64_t /*size*/, uint64_t /*alignment*/ = 0) override {
-        if (mode_ == Mode::kThrowBadAlloc) {
-            throw std::bad_alloc();
+    SizeLimitedMemoryPool(uint64_t max_allocation_size, FailureMode failure_mode)
+        : max_allocation_size_(max_allocation_size),
+          failure_mode_(failure_mode),
+          delegate_(GetMemoryPool()) {}
+
+    void* Malloc(uint64_t size, uint64_t alignment) override {
+        if (size > max_allocation_size_) {
+            return FailAllocation();
         }
-        return nullptr;
+        return delegate_->Malloc(size, alignment);
     }
 
-    void* Realloc(void* /*p*/, size_t /*old_size*/, size_t /*new_size*/,
-                  uint64_t /*alignment*/ = 0) override {
-        if (mode_ == Mode::kThrowBadAlloc) {
-            throw std::bad_alloc();
+    void* Realloc(void* p, size_t old_size, size_t new_size, uint64_t alignment) override {
+        if (new_size > max_allocation_size_) {
+            return FailAllocation();
         }
-        return nullptr;
+        return delegate_->Realloc(p, old_size, new_size, alignment);
     }
 
-    void Free(void* /*p*/, uint64_t /*size*/) override {}
+    void Free(void* p, uint64_t size) override {
+        delegate_->Free(p, size);
+    }
     uint64_t CurrentUsage() const override {
-        return 0;
+        return delegate_->CurrentUsage();
     }
     uint64_t MaxMemoryUsage() const override {
-        return 0;
+        return delegate_->MaxMemoryUsage();
     }
 
  private:
-    Mode mode_;
+    void* FailAllocation() const {
+        if (failure_mode_ == FailureMode::kThrowBadAlloc) {
+            throw MemoryBudgetExceeded();
+        }
+        return nullptr;
+    }
+
+    const uint64_t max_allocation_size_;
+    const FailureMode failure_mode_;
+    std::unique_ptr<MemoryPool> delegate_;
 };
 
 class TrackingMemoryPool : public MemoryPool {
@@ -176,32 +195,52 @@
     ASSERT_EQ(50, pool->max_memory());
 }
 
-TEST(MemUtilsTest, TestAllocateOutOfMemory) {
-    uint8_t* ptr = nullptr;
+TEST(MemUtilsTest, TestOutOfMemory) {
+    constexpr int64_t kOldSize = 64;
+    constexpr int64_t kNewSize = 128;
+    using FailureMode = SizeLimitedMemoryPool::FailureMode;
+    const struct {
+        FailureMode failure_mode;
+        const char* allocate_message;
+        const char* reallocate_from_empty_message;
+        const char* resize_message;
+    } cases[] = {
+        {FailureMode::kReturnNull, "failed to allocate 128 bytes",
+         "failed to reallocate memory from 0 to 128 bytes",
+         "failed to reallocate memory from 64 to 128 bytes"},
+        {FailureMode::kThrowBadAlloc, "failed to allocate 128 bytes: test memory budget exhausted",
+         "failed to reallocate memory from 0 to 128 bytes: test memory budget exhausted",
+         "failed to reallocate memory from 64 to 128 bytes: test memory budget exhausted"},
+    };
 
-    // Underlying pool returns nullptr for a positive size.
-    auto null_pool =
-        GetArrowPool(std::make_shared<FailingMemoryPool>(FailingMemoryPool::Mode::kReturnNull));
-    ASSERT_TRUE(null_pool->Allocate(16, 64, &ptr).IsOutOfMemory());
+    for (const auto& test_case : cases) {
+        SCOPED_TRACE(test_case.allocate_message);
+        auto pool =
+            GetArrowPool(std::make_shared<SizeLimitedMemoryPool>(kOldSize, test_case.failure_mode));
+        uint8_t* ptr = nullptr;
+        const arrow::Status allocate_status = pool->Allocate(kNewSize, &ptr);
+        ASSERT_TRUE(allocate_status.IsOutOfMemory()) << allocate_status.ToString();
+        EXPECT_EQ(test_case.allocate_message, allocate_status.message());
+        ASSERT_EQ(nullptr, ptr);
+        ASSERT_EQ(0, pool->bytes_allocated());
 
-    // Underlying pool throws std::bad_alloc.
-    auto throw_pool =
-        GetArrowPool(std::make_shared<FailingMemoryPool>(FailingMemoryPool::Mode::kThrowBadAlloc));
-    ASSERT_TRUE(throw_pool->Allocate(16, 64, &ptr).IsOutOfMemory());
-}
+        const arrow::Status reallocate_status = pool->Reallocate(0, kNewSize, &ptr);
+        ASSERT_TRUE(reallocate_status.IsOutOfMemory()) << reallocate_status.ToString();
+        EXPECT_EQ(test_case.reallocate_from_empty_message, reallocate_status.message());
+        ASSERT_EQ(nullptr, ptr);
+        ASSERT_EQ(0, pool->bytes_allocated());
 
-TEST(MemUtilsTest, TestReallocateOutOfMemory) {
-    uint8_t* ptr = nullptr;
-
-    // Underlying pool returns nullptr for a positive new size.
-    auto null_pool =
-        GetArrowPool(std::make_shared<FailingMemoryPool>(FailingMemoryPool::Mode::kReturnNull));
-    ASSERT_TRUE(null_pool->Reallocate(/*old_size=*/0, /*new_size=*/16, 64, &ptr).IsOutOfMemory());
-
-    // Underlying pool throws std::bad_alloc.
-    auto throw_pool =
-        GetArrowPool(std::make_shared<FailingMemoryPool>(FailingMemoryPool::Mode::kThrowBadAlloc));
-    ASSERT_TRUE(throw_pool->Reallocate(/*old_size=*/0, /*new_size=*/16, 64, &ptr).IsOutOfMemory());
+        auto buffer_result = arrow::AllocateResizableBuffer(kOldSize, pool.get());
+        ASSERT_TRUE(buffer_result.ok()) << buffer_result.status().ToString();
+        auto buffer = std::move(buffer_result).ValueOrDie();
+        const uint8_t* original_data = buffer->data();
+        const arrow::Status resize_status = buffer->Resize(kNewSize);
+        ASSERT_TRUE(resize_status.IsOutOfMemory()) << resize_status.ToString();
+        EXPECT_EQ(test_case.resize_message, resize_status.message());
+        ASSERT_EQ(original_data, buffer->data());
+        ASSERT_EQ(kOldSize, buffer->size());
+        ASSERT_EQ(kOldSize, pool->bytes_allocated());
+    }
 }
 
 TEST(MemUtilsTest, TestAddArrowArrayLifetimeComposesReleaseChain) {